diff --git a/src/Avalonia.Xaml.Interactions/Core/InvokeCommandAction.cs b/src/Avalonia.Xaml.Interactions/Core/InvokeCommandAction.cs index 2720133a7..8a82b467d 100644 --- a/src/Avalonia.Xaml.Interactions/Core/InvokeCommandAction.cs +++ b/src/Avalonia.Xaml.Interactions/Core/InvokeCommandAction.cs @@ -1,101 +1,10 @@ -using System.Windows.Input; -using Avalonia.Data.Converters; - -namespace Avalonia.Xaml.Interactions.Core; +namespace Avalonia.Xaml.Interactions.Core; /// /// Executes a specified when invoked. /// -public class InvokeCommandAction : Interactivity.StyledElementAction +public class InvokeCommandAction : InvokeCommandActionBase { - /// - /// Identifies the avalonia property. - /// - public static readonly StyledProperty CommandProperty = - AvaloniaProperty.Register(nameof(Command)); - - /// - /// Identifies the avalonia property. - /// - public static readonly StyledProperty CommandParameterProperty = - AvaloniaProperty.Register(nameof(CommandParameter)); - - /// - /// Identifies the avalonia property. - /// - public static readonly StyledProperty InputConverterProperty = - AvaloniaProperty.Register(nameof(InputConverter)); - - /// - /// Identifies the avalonia property. - /// - public static readonly StyledProperty InputConverterParameterProperty = - AvaloniaProperty.Register(nameof(InputConverterParameter)); - - /// - /// Identifies the avalonia property. - /// - /// The string.Empty used for default value string means the invariant culture. - public static readonly StyledProperty InputConverterLanguageProperty = - AvaloniaProperty.Register(nameof(InputConverterLanguage), string.Empty); - - /// - /// Gets or sets the command this action should invoke. This is an avalonia property. - /// - public ICommand? Command - { - get => GetValue(CommandProperty); - set => SetValue(CommandProperty, value); - } - - /// - /// Gets or sets the parameter that is passed to . - /// If this is not set, the parameter from the method will be used. - /// This is an optional avalonia property. - /// - public object? CommandParameter - { - get => GetValue(CommandParameterProperty); - set => SetValue(CommandParameterProperty, value); - } - - /// - /// Gets or sets the converter that is run on the parameter from the method. - /// This is an optional avalonia property. - /// - public IValueConverter? InputConverter - { - get => GetValue(InputConverterProperty); - set => SetValue(InputConverterProperty, value); - } - - /// - /// Gets or sets the parameter that is passed to the - /// method of . - /// This is an optional avalonia property. - /// - public object? InputConverterParameter - { - get => GetValue(InputConverterParameterProperty); - set => SetValue(InputConverterParameterProperty, value); - } - - /// - /// Gets or sets the language that is passed to the - /// method of . - /// This is an optional avalonia property. - /// - public string? InputConverterLanguage - { - get => GetValue(InputConverterLanguageProperty); - set => SetValue(InputConverterLanguageProperty, value); - } - - /// - /// Specifies whether the EventArgs of the event that triggered this action should be passed to the Command as a parameter. - /// - public bool PassEventArgsToCommand { get; set; } - /// /// Executes the action. /// @@ -109,29 +18,7 @@ public override object Execute(object? sender, object? parameter) return false; } - object? resolvedParameter = default; - if (IsSet(CommandParameterProperty)) - { - resolvedParameter = CommandParameter; - } - else if (InputConverter is not null) - { - resolvedParameter = InputConverter.Convert( - parameter, - typeof(object), - InputConverterParameter, - InputConverterLanguage is not null - ? - new System.Globalization.CultureInfo(InputConverterLanguage) - : System.Globalization.CultureInfo.CurrentCulture); - } - else - { - if (PassEventArgsToCommand) - { - resolvedParameter = parameter; - } - } + var resolvedParameter = ResolveParameter(parameter); if (!Command.CanExecute(resolvedParameter)) { @@ -139,6 +26,7 @@ InputConverterLanguage is not null } Command.Execute(resolvedParameter); + return true; } } diff --git a/src/Avalonia.Xaml.Interactions/Core/InvokeCommandActionBase.cs b/src/Avalonia.Xaml.Interactions/Core/InvokeCommandActionBase.cs new file mode 100644 index 000000000..01a320abe --- /dev/null +++ b/src/Avalonia.Xaml.Interactions/Core/InvokeCommandActionBase.cs @@ -0,0 +1,133 @@ +using System.Windows.Input; +using Avalonia.Data.Converters; +using Avalonia.Xaml.Interactivity; + +namespace Avalonia.Xaml.Interactions.Core; + +/// +/// Command action base class. +/// +public abstract class InvokeCommandActionBase : StyledElementAction +{ + /// + /// Identifies the avalonia property. + /// + public static readonly StyledProperty CommandProperty = + AvaloniaProperty.Register(nameof(Command)); + + /// + /// Identifies the avalonia property. + /// + public static readonly StyledProperty CommandParameterProperty = + AvaloniaProperty.Register(nameof(CommandParameter)); + + /// + /// Identifies the avalonia property. + /// + public static readonly StyledProperty InputConverterProperty = + AvaloniaProperty.Register(nameof(InputConverter)); + + /// + /// Identifies the avalonia property. + /// + public static readonly StyledProperty InputConverterParameterProperty = + AvaloniaProperty.Register(nameof(InputConverterParameter)); + + /// + /// Identifies the avalonia property. + /// + /// The string.Empty used for default value string means the invariant culture. + public static readonly StyledProperty InputConverterLanguageProperty = + AvaloniaProperty.Register(nameof(InputConverterLanguage), string.Empty); + + /// + /// Gets or sets the command this action should invoke. This is an avalonia property. + /// + public ICommand? Command + { + get => GetValue(CommandProperty); + set => SetValue(CommandProperty, value); + } + + /// + /// Gets or sets the parameter that is passed to . + /// If this is not set, the parameter from the method will be used. + /// This is an optional avalonia property. + /// + public object? CommandParameter + { + get => GetValue(CommandParameterProperty); + set => SetValue(CommandParameterProperty, value); + } + + /// + /// Gets or sets the converter that is run on the parameter from the method. + /// This is an optional avalonia property. + /// + public IValueConverter? InputConverter + { + get => GetValue(InputConverterProperty); + set => SetValue(InputConverterProperty, value); + } + + /// + /// Gets or sets the parameter that is passed to the + /// method of . + /// This is an optional avalonia property. + /// + public object? InputConverterParameter + { + get => GetValue(InputConverterParameterProperty); + set => SetValue(InputConverterParameterProperty, value); + } + + /// + /// Gets or sets the language that is passed to the + /// method of . + /// This is an optional avalonia property. + /// + public string? InputConverterLanguage + { + get => GetValue(InputConverterLanguageProperty); + set => SetValue(InputConverterLanguageProperty, value); + } + + /// + /// Specifies whether the EventArgs of the event that triggered this action should be passed to the Command as a parameter. + /// + public bool PassEventArgsToCommand { get; set; } + + /// + /// Resolves the command parameter. + /// + /// The parameter. + /// + protected object? ResolveParameter(object? parameter) + { + object? resolvedParameter = null; + if (IsSet(CommandParameterProperty)) + { + resolvedParameter = CommandParameter; + } + else if (InputConverter is not null) + { + resolvedParameter = InputConverter.Convert( + parameter, + typeof(object), + InputConverterParameter, + InputConverterLanguage is not null + ? + new System.Globalization.CultureInfo(InputConverterLanguage) + : System.Globalization.CultureInfo.CurrentCulture); + } + else + { + if (PassEventArgsToCommand) + { + resolvedParameter = parameter; + } + } + + return resolvedParameter; + } +}