Improve navigation

This commit is contained in:
TheXamlGuy
2024-05-16 22:30:01 +01:00
parent 7f3c4c53cc
commit 85a14a8259
9 changed files with 53 additions and 38 deletions
+6 -3
View File
@@ -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);
}
+2 -2
View File
@@ -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<TNavigation>(object Context,
public record NavigateEventArgs<TNavigation>(object Region,
object Template,
object Content,
object? Sender = null,
+1 -1
View File
@@ -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;
}
+24 -13
View File
@@ -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)