Switch out dotnet ctk messenger for source gen Meditor
This commit is contained in:
@@ -3,17 +3,17 @@ 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.Data;
|
||||
message.Target.Content = control;
|
||||
}
|
||||
//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);
|
||||
}
|
||||
}
|
||||
// message.Reply(true);
|
||||
// }
|
||||
//}
|
||||
}
|
||||
|
||||
@@ -2,51 +2,51 @@
|
||||
|
||||
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();
|
||||
//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 INavigationConfirmationAsync confirmationAsync)
|
||||
// {
|
||||
// if (!await confirmationAsync.CanConfirmAsync())
|
||||
// {
|
||||
// args.Cancel = true;
|
||||
// }
|
||||
// }
|
||||
|
||||
if (sender.DataContext is INavigationConfirmation confirmation)
|
||||
{
|
||||
if (!confirmation.CanConfirm())
|
||||
{
|
||||
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;
|
||||
}
|
||||
// if (!args.Cancel)
|
||||
// {
|
||||
// contentDialog.SecondaryButtonClick -= HandleButtonClick;
|
||||
// contentDialog.PrimaryButtonClick -= HandleButtonClick;
|
||||
// contentDialog.CloseButtonClick -= HandleButtonClick;
|
||||
// }
|
||||
|
||||
defferal.Complete();
|
||||
}
|
||||
// defferal.Complete();
|
||||
// }
|
||||
|
||||
contentDialog.SecondaryButtonClick += HandleButtonClick;
|
||||
contentDialog.PrimaryButtonClick += HandleButtonClick;
|
||||
contentDialog.CloseButtonClick += HandleButtonClick;
|
||||
// contentDialog.SecondaryButtonClick += HandleButtonClick;
|
||||
// contentDialog.PrimaryButtonClick += HandleButtonClick;
|
||||
// contentDialog.CloseButtonClick += HandleButtonClick;
|
||||
|
||||
contentDialog.DataContext = message.Data;
|
||||
await contentDialog.ShowAsync();
|
||||
// contentDialog.DataContext = message.Content;
|
||||
// await contentDialog.ShowAsync();
|
||||
|
||||
message.Reply(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
// message.Reply(true);
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
}
|
||||
|
||||
@@ -4,31 +4,30 @@ 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();
|
||||
//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.Data;
|
||||
completionSource.SetResult(true);
|
||||
}
|
||||
}
|
||||
// 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.Target.Navigated += HandleNavigated;
|
||||
// message.Target.NavigateFromObject(content);
|
||||
// }
|
||||
|
||||
bool result = await completionSource.Task;
|
||||
message.Reply(result);
|
||||
}
|
||||
}
|
||||
// message.Reply(await completionSource.Task);
|
||||
// }
|
||||
//}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
using Avalonia.Controls.Primitives;
|
||||
|
||||
namespace Toolkit.Foundation.Avalonia
|
||||
{
|
||||
//public interface INavigationRouteHandler<TTarget> where TTarget : TemplatedControl
|
||||
//{
|
||||
// void Handle(NavigationRouteRequest<TTarget> message);
|
||||
//}
|
||||
}
|
||||
@@ -1,10 +1,9 @@
|
||||
using Avalonia.Controls.Primitives;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
|
||||
namespace Toolkit.Foundation.Avalonia
|
||||
{
|
||||
public abstract class NavigationRouteHandler<TTarget> : IRecipient<NavigationRouteRequest<TTarget>> where TTarget : TemplatedControl
|
||||
{
|
||||
public abstract void Receive(NavigationRouteRequest<TTarget> message);
|
||||
}
|
||||
//public abstract class NavigationRouteHandler<TTarget> : IRecipient<NavigationRouteRequest<TTarget>> where TTarget : TemplatedControl
|
||||
//{
|
||||
// public abstract void Receive(NavigationRouteRequest<TTarget> message);
|
||||
//}
|
||||
}
|
||||
|
||||
@@ -1,24 +1,31 @@
|
||||
using Avalonia.Controls.Primitives;
|
||||
using CommunityToolkit.Mvvm.Messaging.Messages;
|
||||
|
||||
namespace Toolkit.Foundation.Avalonia
|
||||
{
|
||||
public class NavigationRouteRequest<TTarget> : AsyncRequestMessage<bool> where TTarget : TemplatedControl
|
||||
{
|
||||
public NavigationRouteRequest(TTarget target, object? data, object? template, IDictionary<string, object>? parameters = null)
|
||||
{
|
||||
Target = target;
|
||||
Data = data;
|
||||
Template = template;
|
||||
Parameters = parameters;
|
||||
}
|
||||
//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 TTarget Target { get; }
|
||||
|
||||
public object? Data { get; }
|
||||
// public object? Content { get; }
|
||||
|
||||
public object? Template { get; }
|
||||
// public object? Template { get; }
|
||||
|
||||
public IDictionary<string, object>? Parameters { 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);
|
||||
// }
|
||||
//}
|
||||
}
|
||||
|
||||
@@ -1,41 +1,62 @@
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Interactivity;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using FluentAvalonia.UI.Controls;
|
||||
using Mediator;
|
||||
|
||||
namespace Toolkit.Foundation.Avalonia
|
||||
{
|
||||
public class NavigationRouter : INavigationRouter
|
||||
{
|
||||
private readonly INavigationRouteDescriptorCollection descriptors;
|
||||
private readonly IMessenger messenger;
|
||||
private readonly IMediator mediator;
|
||||
private readonly INamedDataTemplateFactory namedDataTemplateFactory;
|
||||
private readonly INamedTemplateFactory namedTemplateFactory;
|
||||
private readonly ITemplateDescriptorProvider templateDescriptorProvider;
|
||||
private readonly ITemplateFactory templateFactory;
|
||||
private readonly ITypedDataTemplateFactory typedDataTemplateFactory;
|
||||
|
||||
public NavigationRouter(ITemplateDescriptorProvider templateDescriptorProvider,
|
||||
public NavigationRouter(IMediator mediator,
|
||||
ITemplateFactory templateFactory,
|
||||
INamedTemplateFactory namedTemplateFactory,
|
||||
INamedDataTemplateFactory namedDataTemplateFactory,
|
||||
ITypedDataTemplateFactory typedDataTemplateFactory,
|
||||
IMessenger messenger,
|
||||
INavigationRouteDescriptorCollection descriptors)
|
||||
{
|
||||
this.templateDescriptorProvider = templateDescriptorProvider;
|
||||
this.mediator = mediator;
|
||||
this.templateFactory = templateFactory;
|
||||
this.namedTemplateFactory = namedTemplateFactory;
|
||||
this.namedDataTemplateFactory = namedDataTemplateFactory;
|
||||
this.typedDataTemplateFactory = typedDataTemplateFactory;
|
||||
this.messenger = messenger;
|
||||
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)
|
||||
{
|
||||
object? data = null;
|
||||
object? content = null;
|
||||
object? template = null;
|
||||
|
||||
Dictionary<string, object> keyedParameters = new();
|
||||
@@ -58,42 +79,34 @@ namespace Toolkit.Foundation.Avalonia
|
||||
|
||||
if (args.Name is { Length: > 0 } name)
|
||||
{
|
||||
data = namedDataTemplateFactory.Create(name, parameters.ToArray());
|
||||
content = namedDataTemplateFactory.Create(name, parameters.ToArray());
|
||||
template = namedTemplateFactory.Create(name);
|
||||
}
|
||||
|
||||
if (args.Type is Type type)
|
||||
{
|
||||
data = typedDataTemplateFactory.Create(type, parameters.ToArray());
|
||||
template = templateFactory.Create(data);
|
||||
content = typedDataTemplateFactory.Create(type, parameters.ToArray());
|
||||
template = templateFactory.Create(content);
|
||||
}
|
||||
|
||||
if (template is not null)
|
||||
{
|
||||
bool navigated = false;
|
||||
if (template is ContentDialog contentDialog)
|
||||
object? target = null;
|
||||
if (descriptors.FirstOrDefault(x => args.Route is string { } name && name == x.Name) is NavigationRouteDescriptor descriptor)
|
||||
{
|
||||
navigated = await messenger.Send(new NavigationRouteRequest<ContentDialog>(contentDialog, data, template, keyedParameters));
|
||||
target = descriptor.Route;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (descriptors.FirstOrDefault(x => args.Route is string { } name && name == x.Name) is NavigationRouteDescriptor descriptor)
|
||||
{
|
||||
switch (descriptor.Route)
|
||||
{
|
||||
case Frame frame:
|
||||
navigated = await messenger.Send(new NavigationRouteRequest<Frame>(frame, data, template, keyedParameters));
|
||||
break;
|
||||
case ContentControl contentControl:
|
||||
navigated = await messenger.Send(new NavigationRouteRequest<ContentControl>(contentControl, data, template, keyedParameters));
|
||||
break;
|
||||
}
|
||||
}
|
||||
target = template;
|
||||
}
|
||||
|
||||
if (navigated)
|
||||
if (target is TemplatedControl control)
|
||||
{
|
||||
messenger.Send((Navigated)Navigated.Create((dynamic?)template, (dynamic?)data, keyedParameters));
|
||||
//if (await messenger.Send(NavigationRouteRequest.Create(control, content, template, keyedParameters)))
|
||||
//{
|
||||
// messenger.Send(Navigated.Create(template, content, keyedParameters));
|
||||
//}
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -130,24 +143,5 @@ namespace Toolkit.Foundation.Avalonia
|
||||
|
||||
descriptors.Add(new NavigationRouteDescriptor(name, route));
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user