using Avalonia; using Avalonia.Controls; using Avalonia.Controls.Primitives; using Avalonia.Metadata; using Avalonia.Xaml.Interactivity; using Toolkit.Foundation; namespace Toolkit.UI.Avalonia; public class NavigateAction : AvaloniaObject, IAction { public static readonly DirectProperty ParameterBindingsProperty = AvaloniaProperty.RegisterDirect(nameof(ParameterBindings), x => x.ParameterBindings); public static readonly StyledProperty ParametersProperty = AvaloniaProperty.Register(nameof(Parameters)); public static readonly StyledProperty RegionProperty = AvaloniaProperty.Register(nameof(Region)); public static readonly StyledProperty RouteProperty = AvaloniaProperty.Register(nameof(Route)); public static readonly StyledProperty ScopeProperty = AvaloniaProperty.Register(nameof(Scope)); private ParameterBindingCollection parameterCollection = []; public event EventHandler? Navigated; public object Region { get => GetValue(RegionProperty); set => SetValue(RegionProperty, value); } [Content] public ParameterBindingCollection ParameterBindings => parameterCollection ??= []; public object[]? Parameters { get => GetValue(ParametersProperty); set => SetValue(ParametersProperty, value); } public string Route { get => GetValue(RouteProperty); set => SetValue(RouteProperty, value); } public string Scope { get => GetValue(ScopeProperty); set => SetValue(ScopeProperty, value); } public object Execute(object? sender, object? parameter) { if (sender is Control content) { Dictionary arguments = new(StringComparer.InvariantCultureIgnoreCase); if (content.DataContext is IObservableViewModel observableViewModel) { object[] parameters = [.. Parameters ?? Enumerable.Empty(), .. ParameterBindings is { Count: > 0 } ? ParameterBindings.Select(binding => new KeyValuePair(binding.Key, binding.Value)).ToArray() : Enumerable.Empty>()]; observableViewModel.Publisher.Publish(new NavigateEventArgs(Route, Region == this ? content : Region, Scope ?? null, content.DataContext, Navigated, parameters)); } } return true; } }