Rework navigation so we can resolve by key now

This commit is contained in:
Dan Clark
2024-11-17 23:09:26 +00:00
parent 796ef41e3f
commit 7a9028bbeb
26 changed files with 371 additions and 88 deletions
+13
View File
@@ -0,0 +1,13 @@
using Microsoft.Xaml.Interactivity;
namespace Toolkit.UI.WinUI;
public class AttachedBehaviour :
Trigger
{
protected override void OnAttached()
{
Interaction.ExecuteActions(AssociatedObject, Actions, null);
base.OnAttached();
}
}
+84
View File
@@ -0,0 +1,84 @@
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;
}
}
+53
View File
@@ -0,0 +1,53 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.Xaml.Interactivity;
using Toolkit.Foundation;
using Windows.UI.Xaml.Markup;
namespace Toolkit.UI.WinUI;
[ContentProperty(Name = nameof(Actions))]
public class NavigateRegionAction :
DependencyObject,
IAction
{
public static readonly DependencyProperty ActionsProperty =
DependencyProperty.Register(nameof(Actions),
typeof(ActionCollection), typeof(NavigateRegionAction),
new PropertyMetadata(null));
public static readonly DependencyProperty NameProperty =
DependencyProperty.Register(nameof(Name),
typeof(string), typeof(NavigateRegionAction),
new PropertyMetadata(null));
private ActionCollection? actions;
public ActionCollection Actions => actions ??= [];
public string Name
{
get => (string)GetValue(NameProperty);
set => SetValue(NameProperty, value);
}
public object? Execute(object? sender,
object? parameter)
{
if (sender is Control control)
{
if (control.DataContext is IObservableViewModel observableViewModel)
{
if (observableViewModel.Provider.GetRequiredService<INavigationRegion>()
is INavigationRegion navigationRegion)
{
navigationRegion.Register(Name, sender);
Interaction.ExecuteActions(sender, Actions, parameter);
}
}
}
return true;
}
}
+30
View File
@@ -0,0 +1,30 @@
using Microsoft.UI.Xaml;
using System.Xml.Linq;
namespace Toolkit.WinUI;
public class Parameter :
DependencyObject
{
public static readonly DependencyProperty KeyProperty =
DependencyProperty.Register(nameof(Key),
typeof(object), typeof(Parameter),
new PropertyMetadata(null));
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register(nameof(Value),
typeof(string), typeof(Parameter),
new PropertyMetadata(null));
public string Key
{
get => (string)GetValue(KeyProperty);
set => SetValue(KeyProperty, value);
}
public object Value
{
get => GetValue(ValueProperty);
set => SetValue(ValueProperty, value);
}
}
+6
View File
@@ -0,0 +1,6 @@
using System.Collections.ObjectModel;
namespace Toolkit.WinUI;
public class ParameterCollection :
ObservableCollection<Parameter>;
+1
View File
@@ -19,6 +19,7 @@
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26100.1742" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.6.240923002" />
<PackageReference Include="WinUIEx" Version="2.4.2" />
<PackageReference Include="Microsoft.Xaml.Behaviors.WinUI.Managed" Version="2.0.9" />
</ItemGroup>
<ItemGroup>