Add some initial navigation route handlers
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
using Avalonia.Platform;
|
||||
using Avalonia;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Avalonia.Controls;
|
||||
using FluentAvalonia.UI.Controls;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
|
||||
namespace Toolkit.Foundation.Avalonia
|
||||
@@ -8,7 +10,10 @@ namespace Toolkit.Foundation.Avalonia
|
||||
public static IServiceCollection AddNavigation(this IServiceCollection serviceCollection)
|
||||
{
|
||||
serviceCollection.TryAddSingleton<INavigationRouteDescriptorCollection, NavigationRouteDescriptorCollection>();
|
||||
serviceCollection.TryAddSingleton<INavigationRouter, NavigationRouter>();
|
||||
|
||||
serviceCollection.TryAddTransient<Navigation<Frame>, FrameNavigation>();
|
||||
serviceCollection.TryAddTransient<Navigation<ContentDialog>, ContentDialogNavigation>();
|
||||
serviceCollection.TryAddTransient<Navigation<ContentControl>, ContentControlNavigation>();
|
||||
|
||||
return serviceCollection;
|
||||
}
|
||||
|
||||
@@ -2,13 +2,14 @@
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Data;
|
||||
using Avalonia.Markup.Xaml;
|
||||
using Mediator;
|
||||
|
||||
namespace Toolkit.Foundation.Avalonia
|
||||
{
|
||||
public class NavigateExtension : TriggerExtension
|
||||
{
|
||||
private static readonly AttachedProperty<INavigationRouter> NavigationRouterProperty =
|
||||
AvaloniaProperty.RegisterAttached<NavigateExtension, Control, INavigationRouter>("NavigationRouter");
|
||||
private static readonly AttachedProperty<IMediator> MediatorProperty =
|
||||
AvaloniaProperty.RegisterAttached<NavigateExtension, Control, IMediator>("Mediator");
|
||||
|
||||
private static readonly AttachedProperty<object> ParameterProperty =
|
||||
AvaloniaProperty.RegisterAttached<NavigateExtension, Control, object>("Parameter");
|
||||
@@ -19,48 +20,48 @@ namespace Toolkit.Foundation.Avalonia
|
||||
private static readonly AvaloniaProperty ToProperty =
|
||||
AvaloniaProperty.RegisterAttached<NavigateExtension, Control, object>("To");
|
||||
|
||||
private readonly Binding? navigationRouterBinding;
|
||||
private readonly Binding? mediatorBinding;
|
||||
private readonly List<object?> parameters = new();
|
||||
private readonly Binding? toBinding;
|
||||
private object? route;
|
||||
private Binding? routeBinding;
|
||||
|
||||
public NavigateExtension(object navigationRouter,
|
||||
public NavigateExtension(object mediator,
|
||||
object to)
|
||||
{
|
||||
navigationRouterBinding = navigationRouter.ToBinding();
|
||||
mediatorBinding = mediator.ToBinding();
|
||||
this.toBinding = to is Binding toBinding ? toBinding : to.ToBinding();
|
||||
}
|
||||
|
||||
public NavigateExtension(object navigationRouter,
|
||||
public NavigateExtension(object mediator,
|
||||
object to,
|
||||
object args1)
|
||||
{
|
||||
navigationRouterBinding = navigationRouter.ToBinding();
|
||||
mediatorBinding = mediator.ToBinding();
|
||||
this.toBinding = to is Binding toBinding ? toBinding : to.ToBinding();
|
||||
|
||||
parameters.Add(args1 is MarkupExtension ? args1 : args1.ToBinding());
|
||||
}
|
||||
|
||||
public NavigateExtension(object navigationRouter,
|
||||
public NavigateExtension(object mediator,
|
||||
object to,
|
||||
object args1,
|
||||
object args2)
|
||||
{
|
||||
navigationRouterBinding = navigationRouter.ToBinding();
|
||||
mediatorBinding = mediator.ToBinding();
|
||||
this.toBinding = to is Binding toBinding ? toBinding : to.ToBinding();
|
||||
|
||||
parameters.Add(args1 is MarkupExtension ? args1 : args1.ToBinding());
|
||||
parameters.Add(args2 is MarkupExtension ? args2 : args2.ToBinding());
|
||||
}
|
||||
|
||||
public NavigateExtension(object navigationRouter,
|
||||
public NavigateExtension(object mediator,
|
||||
object to,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3)
|
||||
{
|
||||
navigationRouterBinding = navigationRouter.ToBinding();
|
||||
mediatorBinding = mediator.ToBinding();
|
||||
this.toBinding = to is Binding toBinding ? toBinding : to.ToBinding();
|
||||
|
||||
parameters.Add(args1 is MarkupExtension ? args1 : args1.ToBinding());
|
||||
@@ -68,14 +69,14 @@ namespace Toolkit.Foundation.Avalonia
|
||||
parameters.Add(args3 is MarkupExtension ? args3 : args3.ToBinding());
|
||||
}
|
||||
|
||||
public NavigateExtension(object navigationRouter,
|
||||
public NavigateExtension(object mediator,
|
||||
object to,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4)
|
||||
{
|
||||
navigationRouterBinding = navigationRouter.ToBinding();
|
||||
mediatorBinding = mediator.ToBinding();
|
||||
this.toBinding = to is Binding toBinding ? toBinding : to.ToBinding();
|
||||
|
||||
parameters.Add(args1 is MarkupExtension ? args1 : args1.ToBinding());
|
||||
@@ -84,7 +85,7 @@ namespace Toolkit.Foundation.Avalonia
|
||||
parameters.Add(args4 is MarkupExtension ? args4 : args4.ToBinding());
|
||||
}
|
||||
|
||||
public NavigateExtension(object navigationRouter,
|
||||
public NavigateExtension(object mediator,
|
||||
object to,
|
||||
object args1,
|
||||
object args2,
|
||||
@@ -92,7 +93,7 @@ namespace Toolkit.Foundation.Avalonia
|
||||
object args4,
|
||||
object args5)
|
||||
{
|
||||
navigationRouterBinding = navigationRouter.ToBinding();
|
||||
mediatorBinding = mediator.ToBinding();
|
||||
this.toBinding = to is Binding toBinding ? toBinding : to.ToBinding();
|
||||
|
||||
parameters.Add(args1 is MarkupExtension ? args1 : args1.ToBinding());
|
||||
@@ -102,7 +103,7 @@ namespace Toolkit.Foundation.Avalonia
|
||||
parameters.Add(args5 is MarkupExtension ? args5 : args5.ToBinding());
|
||||
}
|
||||
|
||||
public NavigateExtension(object navigationRouter,
|
||||
public NavigateExtension(object mediator,
|
||||
object to,
|
||||
object args1,
|
||||
object args2,
|
||||
@@ -111,7 +112,7 @@ namespace Toolkit.Foundation.Avalonia
|
||||
object args5,
|
||||
object args6)
|
||||
{
|
||||
navigationRouterBinding = navigationRouter.ToBinding();
|
||||
mediatorBinding = mediator.ToBinding();
|
||||
this.toBinding = to is Binding toBinding ? toBinding : to.ToBinding();
|
||||
|
||||
parameters.Add(args1 is MarkupExtension ? args1 : args1.ToBinding());
|
||||
@@ -122,7 +123,7 @@ namespace Toolkit.Foundation.Avalonia
|
||||
parameters.Add(args6 is MarkupExtension ? args6 : args6.ToBinding());
|
||||
}
|
||||
|
||||
public NavigateExtension(object navigationRouter,
|
||||
public NavigateExtension(object mediator,
|
||||
object to,
|
||||
object args1,
|
||||
object args2,
|
||||
@@ -132,7 +133,7 @@ namespace Toolkit.Foundation.Avalonia
|
||||
object args6,
|
||||
object args7)
|
||||
{
|
||||
navigationRouterBinding = navigationRouter.ToBinding();
|
||||
mediatorBinding = mediator.ToBinding();
|
||||
this.toBinding = to is Binding toBinding ? toBinding : to.ToBinding();
|
||||
|
||||
parameters.Add(args1 is MarkupExtension ? args1 : args1.ToBinding());
|
||||
@@ -144,7 +145,7 @@ namespace Toolkit.Foundation.Avalonia
|
||||
parameters.Add(args7 is MarkupExtension ? args7 : args7.ToBinding());
|
||||
}
|
||||
|
||||
public NavigateExtension(object navigationRouter,
|
||||
public NavigateExtension(object mediator,
|
||||
object to,
|
||||
object args1,
|
||||
object args2,
|
||||
@@ -155,7 +156,7 @@ namespace Toolkit.Foundation.Avalonia
|
||||
object args7,
|
||||
object args8)
|
||||
{
|
||||
navigationRouterBinding = navigationRouter.ToBinding();
|
||||
mediatorBinding = mediator.ToBinding();
|
||||
this.toBinding = to is Binding toBinding ? toBinding : to.ToBinding();
|
||||
|
||||
parameters.Add(args1 is MarkupExtension ? args1 : args1.ToBinding());
|
||||
@@ -168,7 +169,7 @@ namespace Toolkit.Foundation.Avalonia
|
||||
parameters.Add(args8 is MarkupExtension ? args8 : args8.ToBinding());
|
||||
}
|
||||
|
||||
public NavigateExtension(object navigationRouter,
|
||||
public NavigateExtension(object mediator,
|
||||
object to,
|
||||
object args1,
|
||||
object args2,
|
||||
@@ -180,7 +181,7 @@ namespace Toolkit.Foundation.Avalonia
|
||||
object args8,
|
||||
object args9)
|
||||
{
|
||||
navigationRouterBinding = navigationRouter.ToBinding();
|
||||
mediatorBinding = mediator.ToBinding();
|
||||
this.toBinding = to is Binding toBinding ? toBinding : to.ToBinding();
|
||||
|
||||
parameters.Add(args1 is MarkupExtension ? args1 : args1.ToBinding());
|
||||
@@ -194,7 +195,7 @@ namespace Toolkit.Foundation.Avalonia
|
||||
parameters.Add(args9 is MarkupExtension ? args9 : args9.ToBinding());
|
||||
}
|
||||
|
||||
public NavigateExtension(object navigationRouter,
|
||||
public NavigateExtension(object mediator,
|
||||
object to,
|
||||
object args1,
|
||||
object args2,
|
||||
@@ -207,7 +208,7 @@ namespace Toolkit.Foundation.Avalonia
|
||||
object args9,
|
||||
object args10)
|
||||
{
|
||||
navigationRouterBinding = navigationRouter.ToBinding();
|
||||
mediatorBinding = mediator.ToBinding();
|
||||
this.toBinding = to is Binding toBinding ? toBinding : to.ToBinding();
|
||||
|
||||
parameters.Add(args1 is MarkupExtension ? args1 : args1.ToBinding());
|
||||
@@ -222,7 +223,7 @@ namespace Toolkit.Foundation.Avalonia
|
||||
parameters.Add(args10 is MarkupExtension ? args10 : args10.ToBinding());
|
||||
}
|
||||
|
||||
public NavigateExtension(object navigationRouter,
|
||||
public NavigateExtension(object mediator,
|
||||
object to,
|
||||
object args1,
|
||||
object args2,
|
||||
@@ -236,7 +237,7 @@ namespace Toolkit.Foundation.Avalonia
|
||||
object args10,
|
||||
object args11)
|
||||
{
|
||||
navigationRouterBinding = navigationRouter.ToBinding();
|
||||
mediatorBinding = mediator.ToBinding();
|
||||
this.toBinding = to is Binding toBinding ? toBinding : to.ToBinding();
|
||||
|
||||
parameters.Add(args1 is MarkupExtension ? args1 : args1.ToBinding());
|
||||
@@ -252,7 +253,7 @@ namespace Toolkit.Foundation.Avalonia
|
||||
parameters.Add(args11 is MarkupExtension ? args11 : args11.ToBinding());
|
||||
}
|
||||
|
||||
public NavigateExtension(object navigationRouter,
|
||||
public NavigateExtension(object mediator,
|
||||
object to,
|
||||
object args1,
|
||||
object args2,
|
||||
@@ -267,7 +268,7 @@ namespace Toolkit.Foundation.Avalonia
|
||||
object args11,
|
||||
object args12)
|
||||
{
|
||||
navigationRouterBinding = navigationRouter.ToBinding();
|
||||
mediatorBinding = mediator.ToBinding();
|
||||
this.toBinding = to is Binding toBinding ? toBinding : to.ToBinding();
|
||||
|
||||
parameters.Add(args1 is MarkupExtension ? args1 : args1.ToBinding());
|
||||
@@ -284,7 +285,7 @@ namespace Toolkit.Foundation.Avalonia
|
||||
parameters.Add(args12 is MarkupExtension ? args12 : args12.ToBinding());
|
||||
}
|
||||
|
||||
public NavigateExtension(object navigationRouter,
|
||||
public NavigateExtension(object mediator,
|
||||
object to,
|
||||
object args1,
|
||||
object args2,
|
||||
@@ -300,7 +301,7 @@ namespace Toolkit.Foundation.Avalonia
|
||||
object args12,
|
||||
object args13)
|
||||
{
|
||||
navigationRouterBinding = navigationRouter.ToBinding();
|
||||
mediatorBinding = mediator.ToBinding();
|
||||
this.toBinding = to is Binding toBinding ? toBinding : to.ToBinding();
|
||||
|
||||
parameters.Add(args1 is MarkupExtension ? args1 : args1.ToBinding());
|
||||
@@ -318,7 +319,7 @@ namespace Toolkit.Foundation.Avalonia
|
||||
parameters.Add(args13 is MarkupExtension ? args13 : args13.ToBinding());
|
||||
}
|
||||
|
||||
public NavigateExtension(object navigationRouter,
|
||||
public NavigateExtension(object mediator,
|
||||
object to,
|
||||
object args1,
|
||||
object args2,
|
||||
@@ -335,7 +336,7 @@ namespace Toolkit.Foundation.Avalonia
|
||||
object args13,
|
||||
object args14)
|
||||
{
|
||||
navigationRouterBinding = navigationRouter.ToBinding();
|
||||
mediatorBinding = mediator.ToBinding();
|
||||
this.toBinding = to is Binding toBinding ? toBinding : to.ToBinding();
|
||||
|
||||
parameters.Add(args1 is MarkupExtension ? args1 : args1.ToBinding());
|
||||
@@ -354,7 +355,7 @@ namespace Toolkit.Foundation.Avalonia
|
||||
parameters.Add(args14 is MarkupExtension ? args14 : args14.ToBinding());
|
||||
}
|
||||
|
||||
public NavigateExtension(object navigationRouter,
|
||||
public NavigateExtension(object mediator,
|
||||
object to,
|
||||
object args1,
|
||||
object args2,
|
||||
@@ -372,7 +373,7 @@ namespace Toolkit.Foundation.Avalonia
|
||||
object args14,
|
||||
object args15)
|
||||
{
|
||||
navigationRouterBinding = navigationRouter.ToBinding();
|
||||
mediatorBinding = mediator.ToBinding();
|
||||
this.toBinding = to is Binding toBinding ? toBinding : to.ToBinding();
|
||||
|
||||
parameters.Add(args1 is MarkupExtension ? args1 : args1.ToBinding());
|
||||
@@ -413,10 +414,10 @@ namespace Toolkit.Foundation.Avalonia
|
||||
{
|
||||
if (TargetObject is not null)
|
||||
{
|
||||
if (navigationRouterBinding is not null)
|
||||
if (mediatorBinding is not null)
|
||||
{
|
||||
TargetObject.Bind(NavigationRouterProperty, navigationRouterBinding);
|
||||
if (TargetObject.GetValue(NavigationRouterProperty) is INavigationRouter router)
|
||||
TargetObject.Bind(MediatorProperty, mediatorBinding);
|
||||
if (TargetObject.GetValue(MediatorProperty) is IMediator mediator)
|
||||
{
|
||||
if (toBinding is not null)
|
||||
{
|
||||
@@ -464,12 +465,12 @@ namespace Toolkit.Foundation.Avalonia
|
||||
name = string.Format(format, name);
|
||||
}
|
||||
|
||||
router.Navigate(new Navigate(name, parameters.ToArray()) { Route = route });
|
||||
mediator.Send(new Navigate(name, parameters.ToArray()) { Route = route });
|
||||
}
|
||||
|
||||
if (to is Type type)
|
||||
{
|
||||
router.Navigate(new Navigate(type, parameters.ToArray()) { Route = route });
|
||||
mediator.Send(new Navigate(type, parameters.ToArray()) { Route = route });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,20 +4,21 @@ using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Data;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Markup.Xaml;
|
||||
using Mediator;
|
||||
|
||||
namespace Toolkit.Foundation.Avalonia
|
||||
{
|
||||
public class NavigationRouteExtension : MarkupExtension
|
||||
{
|
||||
private static readonly AttachedProperty<object> RouteProperty =
|
||||
AvaloniaProperty.RegisterAttached<NavigationRouteExtension, Control, object>("Route");
|
||||
private static readonly AttachedProperty<IMediator> MediatorProperty =
|
||||
AvaloniaProperty.RegisterAttached<NavigationRouteExtension, Control, IMediator>("Mediator");
|
||||
|
||||
private readonly string name;
|
||||
private readonly Binding? routeBinding;
|
||||
private readonly Binding? mediatorBinding;
|
||||
|
||||
public NavigationRouteExtension(object route, string name)
|
||||
public NavigationRouteExtension(object mediator, string name)
|
||||
{
|
||||
routeBinding = route is Binding toBinding ? toBinding : route.ToBinding();
|
||||
mediatorBinding = mediator is Binding toBinding ? toBinding : mediator.ToBinding();
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@@ -33,50 +34,55 @@ namespace Toolkit.Foundation.Avalonia
|
||||
{
|
||||
if (target.TargetObject is TemplatedControl control)
|
||||
{
|
||||
if (routeBinding is not null)
|
||||
if (!TryGetBinding(control, out object? binding))
|
||||
{
|
||||
if (!TryGetBinding(control, out object? binding))
|
||||
void HandleDataContextChanged(object? sender, EventArgs args)
|
||||
{
|
||||
void HandleDataContextChanged(object? sender, EventArgs args)
|
||||
{
|
||||
if (TryGetBinding(control, out binding))
|
||||
{
|
||||
control.Loaded -= HandleLoaded;
|
||||
control.Bind(RouteProperty, routeBinding);
|
||||
|
||||
if (control?.GetValue(RouteProperty) is INavigationRouter router)
|
||||
{
|
||||
router.Register(name, control);
|
||||
control.ClearValue(RouteProperty);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
control.DataContextChanged += HandleDataContextChanged;
|
||||
|
||||
void HandleLoaded(object? sender, RoutedEventArgs args)
|
||||
if (TryGetBinding(control, out binding))
|
||||
{
|
||||
control.Loaded -= HandleLoaded;
|
||||
if (TryGetBinding(control, out binding))
|
||||
if (mediatorBinding is not null)
|
||||
{
|
||||
control.Bind(RouteProperty, routeBinding);
|
||||
if (control?.GetValue(RouteProperty) is INavigationRouter router)
|
||||
control.Bind(MediatorProperty, mediatorBinding);
|
||||
if (control.GetValue(MediatorProperty) is IMediator mediator)
|
||||
{
|
||||
router.Register(name, control);
|
||||
control.ClearValue(RouteProperty);
|
||||
mediator.Send(new NavigationRoute(name, control));
|
||||
control.ClearValue(MediatorProperty);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
control.Loaded += HandleLoaded;
|
||||
}
|
||||
else
|
||||
|
||||
control.DataContextChanged += HandleDataContextChanged;
|
||||
|
||||
void HandleLoaded(object? sender, RoutedEventArgs args)
|
||||
{
|
||||
control.Bind(RouteProperty, routeBinding);
|
||||
if (control?.GetValue(RouteProperty) is INavigationRouter router)
|
||||
control.Loaded -= HandleLoaded;
|
||||
if (TryGetBinding(control, out binding))
|
||||
{
|
||||
router.Register(name, control);
|
||||
control.ClearValue(RouteProperty);
|
||||
if (mediatorBinding is not null)
|
||||
{
|
||||
control.Bind(MediatorProperty, mediatorBinding);
|
||||
if (control.GetValue(MediatorProperty) is IMediator mediator)
|
||||
{
|
||||
mediator.Send(new NavigationRoute(name, control));
|
||||
control.ClearValue(MediatorProperty);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
control.Loaded += HandleLoaded;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (mediatorBinding is not null)
|
||||
{
|
||||
control.Bind(MediatorProperty, mediatorBinding);
|
||||
if (control.GetValue(MediatorProperty) is IMediator mediator)
|
||||
{
|
||||
mediator.Send(new NavigationRoute(name, control));
|
||||
control.ClearValue(MediatorProperty);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
// }
|
||||
//}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
using Avalonia.Metadata;
|
||||
|
||||
[assembly: XmlnsDefinition("https://github.com/avaloniaui", "Toolkit.Foundation.Avalonia")]
|
||||
@@ -1,9 +1,17 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Mediator;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public static class IServiceCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddHandler<TRequestHandler>(this IServiceCollection serviceCollection)
|
||||
{
|
||||
serviceCollection.TryAdd(new ServiceDescriptor(typeof(TRequestHandler), typeof(TRequestHandler), ServiceLifetime.Transient));
|
||||
return serviceCollection;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddFoundation(this IServiceCollection serviceCollection)
|
||||
{
|
||||
serviceCollection.AddSingleton<IServiceFactory>(provider => new ServiceFactory(provider.GetService, (instanceType, parameters) => ActivatorUtilities.CreateInstance(provider, instanceType, parameters!)));
|
||||
|
||||
@@ -2,12 +2,7 @@
|
||||
{
|
||||
public static class IServiceFactoryExtensions
|
||||
{
|
||||
public static T? Get<T>(this IServiceFactory serviceFactory)
|
||||
{
|
||||
return serviceFactory.Get<T>(typeof(T));
|
||||
}
|
||||
|
||||
public static T Create<T>(this IServiceFactory serviceFactory, params object?[] parameters)
|
||||
public static T? Create<T>(this IServiceFactory serviceFactory, params object?[] parameters)
|
||||
{
|
||||
return serviceFactory.Create<T>(typeof(T), parameters);
|
||||
}
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public interface INavigationRouter : IInitializer
|
||||
{
|
||||
void Navigate(Navigate args);
|
||||
|
||||
void Register(string name, object route);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
namespace Toolkit.Foundation
|
||||
using Mediator;
|
||||
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public record Navigate
|
||||
public record Navigate : IRequest
|
||||
{
|
||||
public Navigate(string name, params object?[] parameters)
|
||||
{
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
namespace Toolkit.Foundation
|
||||
using Mediator;
|
||||
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public record NavigateBack(object Route);
|
||||
public record NavigateBack(object Route) : IRequest;
|
||||
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
//public class NavigateHandler : IRecipient<Navigate>
|
||||
//{
|
||||
// private readonly IMessenger messenger;
|
||||
|
||||
// public NavigateHandler(IMessenger messenger)
|
||||
// {
|
||||
// this.messenger = messenger;
|
||||
// }
|
||||
|
||||
// public void Receive(Navigate request)
|
||||
// {
|
||||
// messenger.Send(request);
|
||||
// }
|
||||
//}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
using Mediator;
|
||||
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public record NavigationRoute(string Name, object Route) : IRequest;
|
||||
}
|
||||
@@ -2,8 +2,8 @@
|
||||
{
|
||||
public interface IServiceFactory
|
||||
{
|
||||
T? Get<T>(Type type);
|
||||
object? Create(Type type, params object?[] parameters);
|
||||
|
||||
T Create<T>(Type type, params object?[] parameters);
|
||||
T? Create<T>(Type type, params object?[] parameters);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public class ServiceCreator<T> : IServiceCreator<T>
|
||||
public class ServiceCreator<I, T> : IServiceCreator<I>
|
||||
{
|
||||
public virtual object Create(Func<Type, object[], object> creator, params object[] parameters)
|
||||
{
|
||||
|
||||
@@ -11,13 +11,13 @@
|
||||
this.creator = creator;
|
||||
}
|
||||
|
||||
public T? Get<T>(Type type)
|
||||
public object? Create(Type type, params object?[] parameters)
|
||||
{
|
||||
T? value = (T?)factory(type);
|
||||
return value;
|
||||
dynamic? lookup = factory(typeof(IServiceCreator<>).MakeGenericType(type));
|
||||
return lookup is not null ? lookup.Create(creator, parameters) : creator(type, parameters);
|
||||
}
|
||||
|
||||
public T Create<T>(Type type, params object?[] parameters)
|
||||
public T? Create<T>(Type type, params object?[] parameters)
|
||||
{
|
||||
dynamic? lookup = factory(typeof(IServiceCreator<>).MakeGenericType(type));
|
||||
return lookup is not null ? (T)lookup.Create(creator, parameters) : (T)creator(type, parameters);
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
|
||||
if (descriptors.FirstOrDefault(x => x.Name == name) is ITemplateDescriptor descriptor)
|
||||
{
|
||||
data = parameters is { Length: > 0 } ? serviceFactory.Create<object>(descriptor.DataType, parameters) : serviceFactory.Get<object>(descriptor.DataType);
|
||||
data = parameters is { Length: > 0 } ? serviceFactory.Create<object>(descriptor.DataType, parameters) : serviceFactory.Create(descriptor.DataType);
|
||||
if (data is ICache cache)
|
||||
{
|
||||
this.cache[name] = cache;
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
|
||||
if (provider.Get(name) is ITemplateDescriptor descriptor)
|
||||
{
|
||||
view = serviceFactory.Get<object>(descriptor.TemplateType);
|
||||
view = serviceFactory.Create(descriptor.TemplateType);
|
||||
if (view is ICache cache)
|
||||
{
|
||||
this.cache[name] = cache;
|
||||
|
||||
@@ -30,7 +30,7 @@ namespace Toolkit.Foundation
|
||||
|
||||
if (provider.Get(data.GetType()) is ITemplateDescriptor descriptor)
|
||||
{
|
||||
template = serviceFactory.Get<object>(descriptor.TemplateType);
|
||||
template = serviceFactory.Create(descriptor.TemplateType);
|
||||
if (template is ICache cache)
|
||||
{
|
||||
this.cache[data] = cache;
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
|
||||
if (descriptors.FirstOrDefault(x => x.DataType == type) is ITemplateDescriptor descriptor)
|
||||
{
|
||||
data = parameters is { Length: > 0 } ? serviceFactory.Create<object>(descriptor.DataType, parameters) : serviceFactory.Get<object>(descriptor.DataType);
|
||||
data = parameters is { Length: > 0 } ? serviceFactory.Create<object>(descriptor.DataType, parameters) : serviceFactory.Create(descriptor.DataType);
|
||||
if (data is ICache cache)
|
||||
{
|
||||
this.cache[type] = cache;
|
||||
|
||||
Reference in New Issue
Block a user