Files
Toolkit2/Toolkit.UI.WinUI/NavigateAction.cs
T
2024-11-17 23:09:26 +00:00

84 lines
2.6 KiB
C#

using CommunityToolkit.Mvvm.Messaging;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.Xaml.Interactivity;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using Toolkit.Foundation;
using Toolkit.WinUI;
using Windows.UI.Xaml.Markup;
namespace Toolkit.UI.WinUI;
[ContentProperty(Name = nameof(Parameters))]
public class NavigateAction :
DependencyObject,
IAction
{
public static readonly DependencyProperty RegionProperty =
DependencyProperty.Register(nameof(Region),
typeof(object), typeof(NavigateAction),
new PropertyMetadata(null));
public static readonly DependencyProperty RouteProperty =
DependencyProperty.Register(nameof(Route),
typeof(string), typeof(NavigateAction),
new PropertyMetadata(null));
public static readonly DependencyProperty ScopeProperty =
DependencyProperty.Register(nameof(Scope),
typeof(string), typeof(NavigateAction),
new PropertyMetadata(null));
public static readonly DependencyProperty ParametersProperty =
DependencyProperty.Register(nameof(Parameters),
typeof(ParameterCollection), typeof(NavigateAction),
new PropertyMetadata(null));
private ParameterCollection parameterCollection = [];
public event EventHandler? Navigated;
public object Region
{
get => GetValue(RegionProperty);
set => SetValue(RegionProperty, value);
}
public ParameterCollection Parameters =>
parameterCollection ??= [];
public string Route
{
get => (string)GetValue(RouteProperty);
set => SetValue(RouteProperty, value);
}
public string Scope
{
get => (string)GetValue(ScopeProperty);
set => SetValue(ScopeProperty, value);
}
public object Execute(object? sender,
object? parameter)
{
if (sender is Control content)
{
Dictionary<string, object> arguments =
new(StringComparer.InvariantCultureIgnoreCase);
if (content.DataContext is IObservableViewModel observableViewModel)
{
ImmutableDictionary<string, object>? parameters = Parameters is { Count: > 0 } ? Parameters.ToImmutableDictionary(x => x.Key, x => x.Value) :
ImmutableDictionary<string, object>.Empty;
observableViewModel.Messenger.Send(new NavigateEventArgs(Route, Region.Equals(this) ? content : Region, Scope ?? null,
content.DataContext, Navigated, parameters));
}
}
return true;
}
}