using System.Diagnostics.Tracing; using System.Windows.Input; using Avalonia.Data.Converters; using CommunityToolkit.Mvvm.Input; namespace Avalonia.Xaml.Interactions.Core; /// /// Executes a specified when invoked. /// public class InvokeCommandAction : Interactivity.Action { /// /// 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 a 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. /// /// The that is passed to the action by the behavior. Generally this is or a target object. /// The value of this parameter is determined by the caller. /// True if the command is successfully executed; else false. public override object Execute(object? sender, object? parameter) { if (IsEnabled != true || Command is null) { 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; } } if (!Command.CanExecute(resolvedParameter)) { return false; } if (Command is IRelayCommand command) command.Execute(sender, resolvedParameter); else Command.Execute(resolvedParameter); return true; } }