diff --git a/Toolkit.Avalonia/ContentControlHandler.cs b/Toolkit.Avalonia/ContentControlHandler.cs index 0c69bc6..46f5c62 100644 --- a/Toolkit.Avalonia/ContentControlHandler.cs +++ b/Toolkit.Avalonia/ContentControlHandler.cs @@ -9,7 +9,7 @@ public class ContentControlHandler : { public async Task Handle(NavigateEventArgs args) { - if (args.Context is ContentControl contentControl) + if (args.Region is ContentControl contentControl) { if (args.Template is Control control) { diff --git a/Toolkit.Avalonia/ContentDialogHandler.cs b/Toolkit.Avalonia/ContentDialogHandler.cs index b7856e6..a637460 100644 --- a/Toolkit.Avalonia/ContentDialogHandler.cs +++ b/Toolkit.Avalonia/ContentDialogHandler.cs @@ -8,7 +8,7 @@ public class ContentDialogHandler(IDispatcher dispatcher) : { public async Task Handle(NavigateEventArgs args) { - if (args.Context is ContentDialog contentDialog) + if (args.Region is ContentDialog contentDialog) { contentDialog.DataContext = args.Content; diff --git a/Toolkit.Avalonia/FrameHandler.cs b/Toolkit.Avalonia/FrameHandler.cs index 04b5ab8..466b7fb 100644 --- a/Toolkit.Avalonia/FrameHandler.cs +++ b/Toolkit.Avalonia/FrameHandler.cs @@ -14,7 +14,7 @@ public class FrameHandler : { public Task Handle(NavigateEventArgs args) { - if (args.Context is Frame frame) + if (args.Region is Frame frame) { frame.NavigationPageFactory ??= new NavigationPageFactory(); if (args.Template is Control control) diff --git a/Toolkit.Foundation/INavigationScope.cs b/Toolkit.Foundation/INavigationScope.cs index b6add22..24223d5 100644 --- a/Toolkit.Foundation/INavigationScope.cs +++ b/Toolkit.Foundation/INavigationScope.cs @@ -2,8 +2,11 @@ public interface INavigationScope { - void Navigate(string route, object? sender = null, object? context = null, - EventHandler? navigated = null, object[]? parameters = null); + void Navigate(string route, + object? sender = null, + object? region = null, + EventHandler? navigated = null, + object[]? parameters = null); - void Back(object? context); + void Back(object? region); } \ No newline at end of file diff --git a/Toolkit.Foundation/NavigateEventArgs.cs b/Toolkit.Foundation/NavigateEventArgs.cs index b6d2ff3..432cd3e 100644 --- a/Toolkit.Foundation/NavigateEventArgs.cs +++ b/Toolkit.Foundation/NavigateEventArgs.cs @@ -1,13 +1,13 @@ namespace Toolkit.Foundation; public record NavigateEventArgs(string Route, - object? Context = null, + object? Region = null, string? Scope = null, object? Sender = null, EventHandler? Navigated = null, object[]? Parameters = null); -public record NavigateEventArgs(object Context, +public record NavigateEventArgs(object Region, object Template, object Content, object? Sender = null, diff --git a/Toolkit.Foundation/NavigateHandler.cs b/Toolkit.Foundation/NavigateHandler.cs index 7706346..0310642 100644 --- a/Toolkit.Foundation/NavigateHandler.cs +++ b/Toolkit.Foundation/NavigateHandler.cs @@ -20,7 +20,7 @@ public class NavigateHandler(NamedComponent scope, } navigationScope?.Navigate(args.Route, args.Sender, - args.Context, args.Navigated, args.Parameters); + args.Region, args.Navigated, args.Parameters); return Task.CompletedTask; } diff --git a/Toolkit.Foundation/NavigationScope.cs b/Toolkit.Foundation/NavigationScope.cs index 2a27790..40b1162 100644 --- a/Toolkit.Foundation/NavigationScope.cs +++ b/Toolkit.Foundation/NavigationScope.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.DependencyInjection; +using System.Data.SqlTypes; namespace Toolkit.Foundation; @@ -6,16 +7,21 @@ public class NavigationScope(IPublisher publisher, IServiceProvider provider, IServiceFactory factory, INavigationProvider navigationProvider, - INavigationRegionProvider navigationContextProvider, + INavigationRegionProvider navigationRegionProvider, IContentTemplateDescriptorProvider contentTemplateDescriptorProvider) : INavigationScope { public void Navigate(string route, object? sender = null, - object? context = null, + object? region = null, EventHandler? navigated = null, object[]? parameters = null) { + if (region is null) + { + return; + } + string[] segments = route.Split('/'); int segmentCount = segments.Length; int currentSegmentIndex = 0; @@ -44,28 +50,33 @@ public class NavigationScope(IPublisher publisher, if (provider.GetRequiredKeyedService(descriptor.TemplateType, segment) is object view) { - if (context is not null) + if (region is not null) { - if (navigationContextProvider.TryGet(context, out object? scopedContext)) + switch (region) { - context = scopedContext; + case "self": + region = view; + break; + default: + if (navigationRegionProvider.TryGet(region, out object? value)) + { + region = value; + } + + break; } } - else - { - context = view; - } - if (context is not null) + if (region is not null) { if ((parameters is { Length: > 0 } ? factory.Create(descriptor.ContentType, parameters) : provider.GetRequiredKeyedService(descriptor.ContentType, segment)) is object viewModel) { - if (navigationProvider.Get(context is Type type ? type : context.GetType()) is INavigation navigation) + if (navigationProvider.Get(region is Type type ? type : region.GetType()) is INavigation navigation) { Type navigateType = typeof(NavigateEventArgs<>).MakeGenericType(navigation.Type); - if (Activator.CreateInstance(navigateType, [context, view, viewModel, sender, parameters]) is object navigate) + if (Activator.CreateInstance(navigateType, [region, view, viewModel, sender, parameters]) is object navigate) { publisher.Publish(navigate); if (currentSegmentIndex == segmentCount) @@ -85,7 +96,7 @@ public class NavigationScope(IPublisher publisher, { if (context is not null) { - navigationContextProvider.TryGet(context, out context); + navigationRegionProvider.TryGet(context, out context); } if (context is not null) diff --git a/Toolkit.UI.Avalonia/NavigateAction.cs b/Toolkit.UI.Avalonia/NavigateAction.cs index 92cc6aa..be04ddc 100644 --- a/Toolkit.UI.Avalonia/NavigateAction.cs +++ b/Toolkit.UI.Avalonia/NavigateAction.cs @@ -1,4 +1,5 @@ using Avalonia; +using Avalonia.Controls; using Avalonia.Controls.Primitives; using Avalonia.Metadata; using Avalonia.Xaml.Interactivity; @@ -10,9 +11,6 @@ public class NavigateAction : AvaloniaObject, IAction { - public static readonly StyledProperty ContextProperty = - AvaloniaProperty.Register(nameof(Context)); - public static readonly DirectProperty ParameterBindingsProperty = AvaloniaProperty.RegisterDirect(nameof(ParameterBindings), x => x.ParameterBindings); @@ -20,6 +18,9 @@ public class NavigateAction : 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)); @@ -30,10 +31,10 @@ public class NavigateAction : public event EventHandler? Navigated; - public object Context + public object Region { - get => GetValue(ContextProperty); - set => SetValue(ContextProperty, value); + get => GetValue(RegionProperty); + set => SetValue(RegionProperty, value); } [Content] @@ -61,12 +62,12 @@ public class NavigateAction : public object Execute(object? sender, object? parameter) { - if (sender is TemplatedControl control) + if (sender is Control content) { Dictionary arguments = new(StringComparer.InvariantCultureIgnoreCase); - if (control.DataContext is IObservableViewModel observableViewModel) + if (content.DataContext is IObservableViewModel observableViewModel) { object[] parameters = [.. Parameters ?? Enumerable.Empty(), .. @@ -74,8 +75,8 @@ public class NavigateAction : ParameterBindings.Select(binding => new KeyValuePair(binding.Key, binding.Value)).ToArray() : Enumerable.Empty>()]; - observableViewModel.Publisher.Publish(new NavigateEventArgs(Route, Context == this ? control : Context, Scope ?? null, - control.DataContext, Navigated, parameters)); + observableViewModel.Publisher.Publish(new NavigateEventArgs(Route, Region == this ? content : Region, Scope ?? null, + content.DataContext, Navigated, parameters)); } } diff --git a/Toolkit.UI.Avalonia/NavigateBackAction.cs b/Toolkit.UI.Avalonia/NavigateBackAction.cs index db925ed..62e6dbd 100644 --- a/Toolkit.UI.Avalonia/NavigateBackAction.cs +++ b/Toolkit.UI.Avalonia/NavigateBackAction.cs @@ -9,16 +9,16 @@ public class NavigateBackAction : AvaloniaObject, IAction { - public static readonly StyledProperty ContextProperty = - AvaloniaProperty.Register(nameof(Context)); + public static readonly StyledProperty RegionProperty = + AvaloniaProperty.Register(nameof(Region)); public static readonly StyledProperty ScopeProperty = AvaloniaProperty.Register(nameof(Scope)); - public string Context + public string Region { - get => GetValue(ContextProperty); - set => SetValue(ContextProperty, value); + get => GetValue(RegionProperty); + set => SetValue(RegionProperty, value); } public string Scope @@ -34,7 +34,7 @@ public class NavigateBackAction : { if (control.DataContext is IObservableViewModel observableViewModel) { - observableViewModel.Publisher.Publish(new NavigateBackEventArgs(Context + observableViewModel.Publisher.Publish(new NavigateBackEventArgs(Region ?? null, Scope ?? null)); } }