Amend Navigation
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
using Avalonia.Controls;
|
||||
using System.Diagnostics;
|
||||
using Toolkit.Controls.Avalonia;
|
||||
using Toolkit.Framework.Foundation;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Toolkit.Framework.Avalonia;
|
||||
|
||||
@@ -31,71 +32,68 @@ public class NavigateHandler : IRequestHandler<Navigate>
|
||||
|
||||
public ValueTask<Unit> Handle(Navigate request, CancellationToken cancellationToken)
|
||||
{
|
||||
object? content = null;
|
||||
object? template = null;
|
||||
|
||||
Dictionary<string, object> keyedParameters = new();
|
||||
List<object> parameters = new();
|
||||
|
||||
foreach (object? parameter in request.Parameters)
|
||||
foreach (NavigationSegment segment in NavigationSegment.FromPath(request.Path))
|
||||
{
|
||||
if (parameter is not null)
|
||||
object? content = null;
|
||||
object? template = null;
|
||||
|
||||
Dictionary<string, object> keyedParameters = new();
|
||||
List<object> parameters = new();
|
||||
|
||||
foreach (object? parameter in request.Parameters)
|
||||
{
|
||||
if (parameter is KeyValuePair<string, object> keyed)
|
||||
if (parameter is not null)
|
||||
{
|
||||
keyedParameters.Add(keyed.Key, keyed.Value);
|
||||
if (parameter is KeyValuePair<string, object> keyed)
|
||||
{
|
||||
keyedParameters.Add(keyed.Key, keyed.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
parameters.Add(parameter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (segment.Name is { Length: > 0 } name)
|
||||
{
|
||||
content = namedContentFactory.Create(name, parameters.ToArray());
|
||||
template = namedContentTemplateFactory.Create(name);
|
||||
}
|
||||
|
||||
if (template is not null)
|
||||
{
|
||||
object? target = null;
|
||||
if (descriptors.FirstOrDefault(x => segment.Target is string { } name && name == x.Name) is NavigationRouteDescriptor descriptor)
|
||||
{
|
||||
target = descriptor.Route;
|
||||
}
|
||||
else
|
||||
{
|
||||
parameters.Add(parameter);
|
||||
target = template;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (request.Name is { Length: > 0 } name)
|
||||
{
|
||||
content = namedContentFactory.Create(name, parameters.ToArray());
|
||||
template = namedContentTemplateFactory.Create(name);
|
||||
}
|
||||
|
||||
if (request.Type is Type type)
|
||||
{
|
||||
content = typedContentFactory.Create(type, parameters.ToArray());
|
||||
template = contentTemplateFactory.Create(content);
|
||||
}
|
||||
|
||||
if (template is not null)
|
||||
{
|
||||
object? target = null;
|
||||
if (descriptors.FirstOrDefault(x => request.Route is string { } name && name == x.Name) is NavigationRouteDescriptor descriptor)
|
||||
{
|
||||
target = descriptor.Route;
|
||||
if (target is Frame frame)
|
||||
{
|
||||
mediator.Send(new FrameNavigation(frame, content, template, keyedParameters));
|
||||
}
|
||||
else if (target is ContentDialog dialog)
|
||||
{
|
||||
mediator.Send(new ContentDialogNavigation(dialog, content, template, keyedParameters));
|
||||
}
|
||||
else if (target is ContentControl contentControl)
|
||||
{
|
||||
mediator.Send(new ContentControlNavigation(contentControl, content, template, keyedParameters));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
target = template;
|
||||
}
|
||||
|
||||
if (target is Frame frame)
|
||||
{
|
||||
mediator.Send(new FrameNavigation(frame, content, template, keyedParameters));
|
||||
}
|
||||
else if (target is ContentDialog dialog)
|
||||
{
|
||||
mediator.Send(new ContentDialogNavigation(dialog, content, template, keyedParameters));
|
||||
}
|
||||
else if (target is ContentControl contentControl)
|
||||
{
|
||||
mediator.Send(new ContentControlNavigation(contentControl, content, template, keyedParameters));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (descriptors.FirstOrDefault(x => request.Route is string { } name && name == x.Name) is NavigationRouteDescriptor descriptor)
|
||||
{
|
||||
if (descriptor.Route is ContentControl contentControl)
|
||||
if (descriptors.FirstOrDefault(x => segment.Target is string { } name && name == x.Name) is NavigationRouteDescriptor descriptor)
|
||||
{
|
||||
contentControl.Content = null;
|
||||
if (descriptor.Route is ContentControl contentControl)
|
||||
{
|
||||
contentControl.Content = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user