Add some initial navigation route handlers
This commit is contained in:
@@ -1,19 +0,0 @@
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
|
||||
namespace Toolkit.Foundation.Avalonia
|
||||
{
|
||||
//public class ContentControlHandler : NavigationRouteHandler<ContentControl>
|
||||
//{
|
||||
// public override void Receive(NavigationRouteRequest<ContentControl> message)
|
||||
// {
|
||||
// if (message.Template is TemplatedControl control)
|
||||
// {
|
||||
// control.DataContext = message.Content;
|
||||
// message.Target.Content = control;
|
||||
// }
|
||||
|
||||
// message.Reply(true);
|
||||
// }
|
||||
//}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using Avalonia.Controls;
|
||||
|
||||
namespace Toolkit.Foundation.Avalonia
|
||||
{
|
||||
public record ContentControlNavigation : Navigation<ContentControl>
|
||||
{
|
||||
public ContentControlNavigation(ContentControl route,
|
||||
object? content,
|
||||
object? template,
|
||||
IDictionary<string, object>? parameters) : base(route, content, template, parameters)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Mediator;
|
||||
|
||||
namespace Toolkit.Foundation.Avalonia
|
||||
{
|
||||
public class ContentControlNavigationHandler : IRequestHandler<ContentControlNavigation, bool>
|
||||
{
|
||||
public async ValueTask<bool> Handle(ContentControlNavigation request, CancellationToken cancellationToken)
|
||||
{
|
||||
if (request.Template is TemplatedControl control)
|
||||
{
|
||||
control.DataContext = request.Content;
|
||||
request.Route.Content = control;
|
||||
}
|
||||
|
||||
return await Task.FromResult(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
using FluentAvalonia.UI.Controls;
|
||||
|
||||
namespace Toolkit.Foundation.Avalonia
|
||||
{
|
||||
//public class ContentDialogHandler : NavigationRouteHandler<ContentDialog>
|
||||
//{
|
||||
// public override async void Receive(NavigationRouteRequest<ContentDialog> message)
|
||||
// {
|
||||
// if (message.Template is ContentDialog contentDialog)
|
||||
// {
|
||||
// async void HandleButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
|
||||
// {
|
||||
// ContentDialogButtonClickDeferral defferal = args.GetDeferral();
|
||||
|
||||
// if (sender.DataContext is INavigationConfirmationAsync confirmationAsync)
|
||||
// {
|
||||
// if (!await confirmationAsync.CanConfirmAsync())
|
||||
// {
|
||||
// args.Cancel = true;
|
||||
// }
|
||||
// }
|
||||
|
||||
// if (sender.DataContext is INavigationConfirmation confirmation)
|
||||
// {
|
||||
// if (!confirmation.CanConfirm())
|
||||
// {
|
||||
// args.Cancel = true;
|
||||
// }
|
||||
// }
|
||||
|
||||
// if (!args.Cancel)
|
||||
// {
|
||||
// contentDialog.SecondaryButtonClick -= HandleButtonClick;
|
||||
// contentDialog.PrimaryButtonClick -= HandleButtonClick;
|
||||
// contentDialog.CloseButtonClick -= HandleButtonClick;
|
||||
// }
|
||||
|
||||
// defferal.Complete();
|
||||
// }
|
||||
|
||||
// contentDialog.SecondaryButtonClick += HandleButtonClick;
|
||||
// contentDialog.PrimaryButtonClick += HandleButtonClick;
|
||||
// contentDialog.CloseButtonClick += HandleButtonClick;
|
||||
|
||||
// contentDialog.DataContext = message.Content;
|
||||
// await contentDialog.ShowAsync();
|
||||
|
||||
// message.Reply(true);
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using FluentAvalonia.UI.Controls;
|
||||
|
||||
namespace Toolkit.Foundation.Avalonia
|
||||
{
|
||||
public record ContentDialogNavigation : Navigation<ContentDialog>
|
||||
{
|
||||
public ContentDialogNavigation(ContentDialog route,
|
||||
object? content,
|
||||
object? template,
|
||||
IDictionary<string, object>? parameters) : base(route, content, template, parameters)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
using FluentAvalonia.UI.Controls;
|
||||
using Mediator;
|
||||
|
||||
namespace Toolkit.Foundation.Avalonia
|
||||
{
|
||||
public class ContentDialogNavigationHandler : IRequestHandler<ContentDialogNavigation, bool>
|
||||
{
|
||||
public async ValueTask<bool> Handle(ContentDialogNavigation request, CancellationToken cancellationToken)
|
||||
{
|
||||
if (request.Template is ContentDialog contentDialog)
|
||||
{
|
||||
async void HandleButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
|
||||
{
|
||||
ContentDialogButtonClickDeferral defferal = args.GetDeferral();
|
||||
|
||||
if (sender.DataContext is INavigationConfirmationAsync confirmationAsync)
|
||||
{
|
||||
if (!await confirmationAsync.CanConfirmAsync())
|
||||
{
|
||||
args.Cancel = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (sender.DataContext is INavigationConfirmation confirmation)
|
||||
{
|
||||
if (!confirmation.CanConfirm())
|
||||
{
|
||||
args.Cancel = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!args.Cancel)
|
||||
{
|
||||
contentDialog.SecondaryButtonClick -= HandleButtonClick;
|
||||
contentDialog.PrimaryButtonClick -= HandleButtonClick;
|
||||
contentDialog.CloseButtonClick -= HandleButtonClick;
|
||||
}
|
||||
|
||||
defferal.Complete();
|
||||
}
|
||||
|
||||
contentDialog.SecondaryButtonClick += HandleButtonClick;
|
||||
contentDialog.PrimaryButtonClick += HandleButtonClick;
|
||||
contentDialog.CloseButtonClick += HandleButtonClick;
|
||||
|
||||
contentDialog.DataContext = request.Content;
|
||||
await contentDialog.ShowAsync();
|
||||
}
|
||||
|
||||
return await Task.FromResult(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
using Avalonia.Controls.Primitives;
|
||||
using FluentAvalonia.UI.Controls;
|
||||
using FluentAvalonia.UI.Navigation;
|
||||
|
||||
namespace Toolkit.Foundation.Avalonia
|
||||
{
|
||||
//public class FrameHandler : NavigationRouteHandler<Frame>
|
||||
//{
|
||||
// public override async void Receive(NavigationRouteRequest<Frame> message)
|
||||
// {
|
||||
// message.Target.NavigationPageFactory = new NavigationPageFactory();
|
||||
|
||||
// TaskCompletionSource<bool> completionSource = new();
|
||||
// if (message.Template is TemplatedControl content)
|
||||
// {
|
||||
// void HandleNavigated(object sender, NavigationEventArgs args)
|
||||
// {
|
||||
// message.Target.Navigated -= HandleNavigated;
|
||||
// if (message.Target.Content is TemplatedControl control)
|
||||
// {
|
||||
// control.DataContext = message.Content;
|
||||
// completionSource.SetResult(true);
|
||||
// }
|
||||
// }
|
||||
|
||||
// message.Target.Navigated += HandleNavigated;
|
||||
// message.Target.NavigateFromObject(content);
|
||||
// }
|
||||
|
||||
// message.Reply(await completionSource.Task);
|
||||
// }
|
||||
//}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using FluentAvalonia.UI.Controls;
|
||||
|
||||
namespace Toolkit.Foundation.Avalonia
|
||||
{
|
||||
public record FrameNavigation : Navigation<Frame>
|
||||
{
|
||||
public FrameNavigation(Frame route,
|
||||
object? content,
|
||||
object? template,
|
||||
IDictionary<string, object>?
|
||||
parameters) : base(route, content, template, parameters)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using Avalonia.Controls.Primitives;
|
||||
using FluentAvalonia.UI.Navigation;
|
||||
using Mediator;
|
||||
|
||||
namespace Toolkit.Foundation.Avalonia
|
||||
{
|
||||
public class FrameNavigationHandler : IRequestHandler<FrameNavigation, bool>
|
||||
{
|
||||
public async ValueTask<bool> Handle(FrameNavigation request, CancellationToken cancellationToken)
|
||||
{
|
||||
request.Route.NavigationPageFactory = new NavigationPageFactory();
|
||||
|
||||
TaskCompletionSource<bool> completionSource = new();
|
||||
if (request.Template is TemplatedControl content)
|
||||
{
|
||||
void HandleNavigated(object sender, NavigationEventArgs args)
|
||||
{
|
||||
request.Route.Navigated -= HandleNavigated;
|
||||
if (request.Route.Content is TemplatedControl control)
|
||||
{
|
||||
control.DataContext = request.Content;
|
||||
completionSource.SetResult(true);
|
||||
}
|
||||
}
|
||||
|
||||
request.Route.Navigated += HandleNavigated;
|
||||
request.Route.NavigateFromObject(content);
|
||||
}
|
||||
|
||||
return await completionSource.Task;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
using Avalonia.Controls.Primitives;
|
||||
|
||||
namespace Toolkit.Foundation.Avalonia
|
||||
{
|
||||
//public interface INavigationRouteHandler<TTarget> where TTarget : TemplatedControl
|
||||
//{
|
||||
// void Handle(NavigationRouteRequest<TTarget> message);
|
||||
//}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using FluentAvalonia.UI.Controls;
|
||||
using Mediator;
|
||||
|
||||
namespace Toolkit.Foundation.Avalonia
|
||||
{
|
||||
public class NavigateBackHandler : IRequestHandler<NavigateBack>
|
||||
{
|
||||
private readonly INavigationRouteDescriptorCollection descriptors;
|
||||
|
||||
public NavigateBackHandler(INavigationRouteDescriptorCollection descriptors)
|
||||
{
|
||||
this.descriptors = descriptors;
|
||||
}
|
||||
|
||||
public ValueTask<Unit> Handle(NavigateBack request, CancellationToken cancellationToken)
|
||||
{
|
||||
if (descriptors.FirstOrDefault(x => request.Route is string { } name && name == x.Name) is NavigationRouteDescriptor descriptor)
|
||||
{
|
||||
if (descriptor.Route is ContentControl { Content: TemplatedControl content })
|
||||
{
|
||||
if (content.DataContext is IDisposable disposable)
|
||||
{
|
||||
disposable.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
if (descriptor.Route is Frame frame)
|
||||
{
|
||||
frame.GoBack();
|
||||
}
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
+24
-64
@@ -1,12 +1,10 @@
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Interactivity;
|
||||
using FluentAvalonia.UI.Controls;
|
||||
using Mediator;
|
||||
|
||||
namespace Toolkit.Foundation.Avalonia
|
||||
{
|
||||
public class NavigationRouter : INavigationRouter
|
||||
public class NavigateHandler : IRequestHandler<Navigate>
|
||||
{
|
||||
private readonly INavigationRouteDescriptorCollection descriptors;
|
||||
private readonly IMediator mediator;
|
||||
@@ -15,7 +13,7 @@ namespace Toolkit.Foundation.Avalonia
|
||||
private readonly ITemplateFactory templateFactory;
|
||||
private readonly ITypedDataTemplateFactory typedDataTemplateFactory;
|
||||
|
||||
public NavigationRouter(IMediator mediator,
|
||||
public NavigateHandler(IMediator mediator,
|
||||
ITemplateFactory templateFactory,
|
||||
INamedTemplateFactory namedTemplateFactory,
|
||||
INamedDataTemplateFactory namedDataTemplateFactory,
|
||||
@@ -30,31 +28,7 @@ namespace Toolkit.Foundation.Avalonia
|
||||
this.descriptors = descriptors;
|
||||
}
|
||||
|
||||
public void GoBack(NavigateBack args)
|
||||
{
|
||||
if (descriptors.FirstOrDefault(x => args.Route is string { } name && name == x.Name) is NavigationRouteDescriptor descriptor)
|
||||
{
|
||||
if (descriptor.Route is ContentControl { Content: TemplatedControl content })
|
||||
{
|
||||
if (content.DataContext is IDisposable disposable)
|
||||
{
|
||||
disposable.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
if (descriptor.Route is Frame frame)
|
||||
{
|
||||
frame.GoBack();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Task InitializeAsync()
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async void Navigate(Navigate args)
|
||||
public async ValueTask<Unit> Handle(Navigate request, CancellationToken cancellationToken)
|
||||
{
|
||||
object? content = null;
|
||||
object? template = null;
|
||||
@@ -62,7 +36,7 @@ namespace Toolkit.Foundation.Avalonia
|
||||
Dictionary<string, object> keyedParameters = new();
|
||||
List<object> parameters = new();
|
||||
|
||||
foreach (object? parameter in args.Parameters)
|
||||
foreach (object? parameter in request.Parameters)
|
||||
{
|
||||
if (parameter is not null)
|
||||
{
|
||||
@@ -77,13 +51,13 @@ namespace Toolkit.Foundation.Avalonia
|
||||
}
|
||||
}
|
||||
|
||||
if (args.Name is { Length: > 0 } name)
|
||||
if (request.Name is { Length: > 0 } name)
|
||||
{
|
||||
content = namedDataTemplateFactory.Create(name, parameters.ToArray());
|
||||
template = namedTemplateFactory.Create(name);
|
||||
}
|
||||
|
||||
if (args.Type is Type type)
|
||||
if (request.Type is Type type)
|
||||
{
|
||||
content = typedDataTemplateFactory.Create(type, parameters.ToArray());
|
||||
template = templateFactory.Create(content);
|
||||
@@ -91,27 +65,34 @@ namespace Toolkit.Foundation.Avalonia
|
||||
|
||||
if (template is not null)
|
||||
{
|
||||
object? target = null;
|
||||
if (descriptors.FirstOrDefault(x => args.Route is string { } name && name == x.Name) is NavigationRouteDescriptor descriptor)
|
||||
object? route = null;
|
||||
if (descriptors.FirstOrDefault(x => request.Route is string { } name && name == x.Name) is NavigationRouteDescriptor descriptor)
|
||||
{
|
||||
target = descriptor.Route;
|
||||
route = descriptor.Route;
|
||||
}
|
||||
else
|
||||
{
|
||||
target = template;
|
||||
route = template;
|
||||
}
|
||||
|
||||
if (target is TemplatedControl control)
|
||||
if (route is Frame frame)
|
||||
{
|
||||
//if (await messenger.Send(NavigationRouteRequest.Create(control, content, template, keyedParameters)))
|
||||
//{
|
||||
// messenger.Send(Navigated.Create(template, content, keyedParameters));
|
||||
//}
|
||||
await mediator.Send(new FrameNavigation(frame, content, template, keyedParameters));
|
||||
}
|
||||
|
||||
if (route is ContentDialog dialog)
|
||||
{
|
||||
await mediator.Send(new ContentDialogNavigation(dialog, content, template, keyedParameters));
|
||||
}
|
||||
|
||||
if (route is ContentControl contentControl)
|
||||
{
|
||||
await mediator.Send(new ContentControlNavigation(contentControl, content, template, keyedParameters));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (descriptors.FirstOrDefault(x => args.Route is string { } name && name == x.Name) is NavigationRouteDescriptor descriptor)
|
||||
if (descriptors.FirstOrDefault(x => request.Route is string { } name && name == x.Name) is NavigationRouteDescriptor descriptor)
|
||||
{
|
||||
if (descriptor.Route is ContentControl contentControl)
|
||||
{
|
||||
@@ -119,29 +100,8 @@ namespace Toolkit.Foundation.Avalonia
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Register(string name, object route)
|
||||
{
|
||||
if (route is TemplatedControl control)
|
||||
{
|
||||
void HandleUnloaded(object? sender, RoutedEventArgs args)
|
||||
{
|
||||
if (descriptors.FirstOrDefault(x => x.Route == sender) is INavigationRouteDescriptor descriptor)
|
||||
{
|
||||
descriptors.Remove(descriptor);
|
||||
}
|
||||
}
|
||||
|
||||
control.Unloaded += HandleUnloaded;
|
||||
}
|
||||
|
||||
if (descriptors.FirstOrDefault(x => x.Name == name) is INavigationRouteDescriptor descriptor)
|
||||
{
|
||||
descriptors.Remove(descriptor);
|
||||
}
|
||||
|
||||
descriptors.Add(new NavigationRouteDescriptor(name, route));
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Mediator;
|
||||
|
||||
namespace Toolkit.Foundation.Avalonia
|
||||
{
|
||||
public record Navigation<TRoute> : IRequest<bool> where TRoute : TemplatedControl
|
||||
{
|
||||
public TRoute Route { get; }
|
||||
|
||||
public Navigation(TRoute route,
|
||||
object? content,
|
||||
object? template,
|
||||
IDictionary<string, object>? parameters)
|
||||
{
|
||||
Route = route;
|
||||
Content = content;
|
||||
Template = template;
|
||||
Parameters = parameters;
|
||||
}
|
||||
|
||||
public object? Content { get; }
|
||||
|
||||
public object? Template { get; }
|
||||
|
||||
public IDictionary<string, object>? Parameters { get; }
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,39 @@
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Interactivity;
|
||||
using Mediator;
|
||||
|
||||
namespace Toolkit.Foundation.Avalonia
|
||||
{
|
||||
//public abstract class NavigationRouteHandler<TTarget> : IRecipient<NavigationRouteRequest<TTarget>> where TTarget : TemplatedControl
|
||||
//{
|
||||
// public abstract void Receive(NavigationRouteRequest<TTarget> message);
|
||||
//}
|
||||
public class NavigationRouteHandler : IRequestHandler<NavigationRoute>
|
||||
{
|
||||
private readonly INavigationRouteDescriptorCollection descriptors;
|
||||
|
||||
public NavigationRouteHandler(INavigationRouteDescriptorCollection descriptors)
|
||||
{
|
||||
this.descriptors = descriptors;
|
||||
}
|
||||
|
||||
public ValueTask<Unit> Handle(NavigationRoute request, CancellationToken cancellationToken)
|
||||
{
|
||||
if (request.Route is TemplatedControl control)
|
||||
{
|
||||
void HandleUnloaded(object? sender, RoutedEventArgs args)
|
||||
{
|
||||
if (descriptors.FirstOrDefault(x => x.Route == sender) is INavigationRouteDescriptor descriptor)
|
||||
{
|
||||
descriptors.Remove(descriptor);
|
||||
}
|
||||
}
|
||||
control.Unloaded += HandleUnloaded;
|
||||
}
|
||||
|
||||
if (descriptors.FirstOrDefault(x => x.Name == request.Name) is INavigationRouteDescriptor descriptor)
|
||||
{
|
||||
descriptors.Remove(descriptor);
|
||||
}
|
||||
|
||||
descriptors.Add(new NavigationRouteDescriptor(request.Name, request.Route));
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
using Avalonia.Controls.Primitives;
|
||||
|
||||
namespace Toolkit.Foundation.Avalonia
|
||||
{
|
||||
//public class NavigationRouteRequest<TTarget> : AsyncRequestMessage<bool> where TTarget : TemplatedControl
|
||||
//{
|
||||
// public NavigationRouteRequest(TTarget target, object? content, object? template, IDictionary<string, object>? parameters = null)
|
||||
// {
|
||||
// Target = target;
|
||||
// Content = content;
|
||||
// Template = template;
|
||||
// Parameters = parameters;
|
||||
// }
|
||||
|
||||
// public TTarget Target { get; }
|
||||
|
||||
// public object? Content { get; }
|
||||
|
||||
// public object? Template { get; }
|
||||
|
||||
// public IDictionary<string, object>? Parameters { get; }
|
||||
//}
|
||||
|
||||
//public class NavigationRouteRequest
|
||||
//{
|
||||
// public static NavigationRouteRequest<TTarget> Create<TTarget>(TTarget target, object? content, object? template, IDictionary<string, object>? parameters = null) where TTarget : TemplatedControl
|
||||
// {
|
||||
// return new NavigationRouteRequest<TTarget>(target, content, template, parameters);
|
||||
// }
|
||||
//}
|
||||
}
|
||||
Reference in New Issue
Block a user