Switch out dotnet ctk messenger for source gen Meditor
This commit is contained in:
@@ -1,10 +1,8 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Data.Converters;
|
||||
using Avalonia.Data.Converters;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Toolkit.Foundation.Avalonia
|
||||
{
|
||||
|
||||
public static class EventArgsExtensions
|
||||
{
|
||||
public static dynamic? GetEventArguments(this EventArgs args, string? path, IValueConverter? converter, object? converterParameter)
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
using Avalonia.Platform;
|
||||
using Avalonia;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace Toolkit.Foundation.Avalonia
|
||||
{
|
||||
public static class IHostBuilderExtensions
|
||||
{
|
||||
public static IHostBuilder ConfigureTemplates(this IHostBuilder hostBuilder, Action<ITemplateBuilder> builderDelegate)
|
||||
{
|
||||
hostBuilder.ConfigureServices((hostBuilderContext, serviceCollection) =>
|
||||
{
|
||||
TemplateBuilder? builder = new();
|
||||
builderDelegate?.Invoke(builder);
|
||||
|
||||
serviceCollection.TryAddSingleton(builder.Descriptors);
|
||||
serviceCollection.TryAddSingleton<ITemplateDescriptorProvider, TemplateDescriptorProvider>();
|
||||
serviceCollection.TryAddSingleton<ITemplateFactory, TemplateFactory>();
|
||||
serviceCollection.TryAddSingleton<INamedTemplateFactory, NamedTemplateFactory>();
|
||||
serviceCollection.TryAddSingleton<ITypedDataTemplateFactory, TypedDataTemplateFactory>();
|
||||
serviceCollection.TryAddSingleton<INamedDataTemplateFactory, NamedDataTemplateFactory>();
|
||||
serviceCollection.TryAddSingleton<ITemplateSelector, TemplateSelector>();
|
||||
|
||||
foreach (ITemplateDescriptor? descriptor in builder.Descriptors)
|
||||
{
|
||||
serviceCollection.Add(new ServiceDescriptor(descriptor.TemplateType, descriptor.TemplateType, descriptor.Lifetime));
|
||||
serviceCollection.Add(new ServiceDescriptor(descriptor.DataType, descriptor.DataType, descriptor.Lifetime));
|
||||
}
|
||||
});
|
||||
|
||||
return hostBuilder;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
|
||||
namespace Toolkit.Foundation.Avalonia
|
||||
{
|
||||
public static class IServiceCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddNavigation(this IServiceCollection serviceCollection)
|
||||
{
|
||||
serviceCollection.TryAddSingleton<INavigationRouteDescriptorCollection, NavigationRouteDescriptorCollection>();
|
||||
serviceCollection.TryAddSingleton<INavigationRouter, NavigationRouter>();
|
||||
|
||||
return serviceCollection;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Avalonia" Version="11.0.0-preview4" />
|
||||
<PackageReference Include="FluentAvaloniaUI" Version="2.0.0-preview4" />
|
||||
<PackageReference Include="Mediator.Abstractions" Version="2.1.0-preview.5" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using Mediator;
|
||||
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public class ConfigurationInitializer<TConfiguration> : IInitializer where TConfiguration : class, new()
|
||||
{
|
||||
private readonly TConfiguration configuration;
|
||||
private readonly IMessenger messenger;
|
||||
private readonly IMediator mediator;
|
||||
|
||||
public ConfigurationInitializer(TConfiguration configuration,
|
||||
IMessenger messenger)
|
||||
IMediator mediator)
|
||||
{
|
||||
this.configuration = configuration;
|
||||
this.messenger = messenger;
|
||||
this.mediator = mediator;
|
||||
}
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
messenger.Send(configuration);
|
||||
await mediator.Send(configuration);
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
namespace Toolkit.Foundation
|
||||
using Mediator;
|
||||
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public record Write<TConfiguration>(string Section, Action<TConfiguration> UpdateDelegate) where TConfiguration : class;
|
||||
public abstract record Write<TConfiguration>(string Section, Action<TConfiguration> UpdateDelegate) : IRequest where TConfiguration : class;
|
||||
}
|
||||
|
||||
@@ -1,28 +1,30 @@
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using Mediator;
|
||||
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public class WriteHandler<TConfiguration> : IRecipient<Write<TConfiguration>> where TConfiguration : class
|
||||
public class WriteHandler<TConfiguration> : IRequestHandler<Write<TConfiguration>> where TConfiguration : class
|
||||
{
|
||||
private readonly IMessenger messenger;
|
||||
private readonly IMediator mediator;
|
||||
private readonly TConfiguration configuration;
|
||||
private readonly IConfigurationWriter<TConfiguration> writer;
|
||||
|
||||
public WriteHandler(TConfiguration configuration,
|
||||
IConfigurationWriter<TConfiguration> writer,
|
||||
IMessenger messenger)
|
||||
IMediator mediator)
|
||||
{
|
||||
this.messenger = messenger;
|
||||
this.mediator = mediator;
|
||||
this.configuration = configuration;
|
||||
this.writer = writer;
|
||||
}
|
||||
|
||||
public void Receive(Write<TConfiguration> request)
|
||||
public async ValueTask<Unit> Handle(Write<TConfiguration> request, CancellationToken cancellationToken)
|
||||
{
|
||||
request.UpdateDelegate.Invoke(configuration);
|
||||
writer.Write(request.Section, configuration);
|
||||
|
||||
messenger.Send(new ConfigurationChanged<TConfiguration>(configuration));
|
||||
await mediator.Send(new ConfigurationChanged<TConfiguration>(configuration), cancellationToken);
|
||||
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public static class IServiceCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddFoundation(this IServiceCollection serviceCollection)
|
||||
{
|
||||
serviceCollection.AddSingleton<IServiceFactory>(provider => new ServiceFactory(provider.GetService, (instanceType, parameters) => ActivatorUtilities.CreateInstance(provider, instanceType, parameters!)));
|
||||
|
||||
serviceCollection
|
||||
.AddSingleton<IInitialization, Initialization>(provider => new Initialization(() =>
|
||||
{
|
||||
return serviceCollection.Where(x => x.ServiceType.GetInterfaces()
|
||||
.Contains(typeof(IInitializer)) || x.ServiceType == typeof(IInitializer))
|
||||
.GroupBy(x => x.ServiceType)
|
||||
.Select(x => x.First())
|
||||
.SelectMany(x => provider.GetServices(x.ServiceType)
|
||||
.Select(x => (IInitializer?)x)).ToList();
|
||||
}));
|
||||
|
||||
return serviceCollection;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,23 @@
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using Mediator;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public class AppService : IHostedService
|
||||
{
|
||||
private readonly IMessenger messenger;
|
||||
private readonly IMediator mediator;
|
||||
private readonly IInitialization initialization;
|
||||
|
||||
public AppService(IMessenger messenger,
|
||||
public AppService(IMediator mediator,
|
||||
IInitialization initialization)
|
||||
{
|
||||
this.messenger = messenger;
|
||||
this.mediator = mediator;
|
||||
this.initialization = initialization;
|
||||
}
|
||||
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
messenger.Send(new Initialize());
|
||||
await mediator.Send(new Initialize());
|
||||
await initialization.InitializeAsync();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
namespace Toolkit.Foundation
|
||||
using Mediator;
|
||||
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public record class Initialize;
|
||||
public record class Initialize : IRequest;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public interface INavigationRouter
|
||||
public interface INavigationRouter : IInitializer
|
||||
{
|
||||
void Navigate(Navigate args);
|
||||
|
||||
|
||||
@@ -1,19 +1,18 @@
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
|
||||
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public class NavigateHandler : IRecipient<Navigate>
|
||||
{
|
||||
private readonly IMessenger messenger;
|
||||
//public class NavigateHandler : IRecipient<Navigate>
|
||||
//{
|
||||
// private readonly IMessenger messenger;
|
||||
|
||||
public NavigateHandler(IMessenger messenger)
|
||||
{
|
||||
this.messenger = messenger;
|
||||
}
|
||||
// public NavigateHandler(IMessenger messenger)
|
||||
// {
|
||||
// this.messenger = messenger;
|
||||
// }
|
||||
|
||||
public void Receive(Navigate request)
|
||||
{
|
||||
messenger.Send(request);
|
||||
}
|
||||
}
|
||||
// public void Receive(Navigate request)
|
||||
// {
|
||||
// messenger.Send(request);
|
||||
// }
|
||||
//}
|
||||
}
|
||||
@@ -1,30 +1,30 @@
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public class Navigated<TContent, TDataContext> where TContent : class where TDataContext : class
|
||||
public class Navigated<TTemplate, TContent> where TTemplate : class where TContent : class
|
||||
{
|
||||
public Navigated()
|
||||
{
|
||||
}
|
||||
|
||||
public Navigated(TContent content, TDataContext dataContext, IDictionary<string, object>? parameters = null)
|
||||
public Navigated(TTemplate template, TContent content, IDictionary<string, object>? parameters = null)
|
||||
{
|
||||
Template = template;
|
||||
Content = content;
|
||||
DataContext = dataContext;
|
||||
Parameters = parameters;
|
||||
}
|
||||
|
||||
public TContent? Content { get; }
|
||||
public TTemplate? Template { get; }
|
||||
|
||||
public TDataContext? DataContext { get; }
|
||||
public TContent? Content { get; }
|
||||
|
||||
public IDictionary<string, object>? Parameters { get; }
|
||||
}
|
||||
|
||||
public class Navigated
|
||||
{
|
||||
public static Navigated<TTemplate, TDataTemplate> Create<TTemplate, TDataTemplate>(TTemplate content, TDataTemplate dataContext, IDictionary<string, object>? parameters = null) where TTemplate : class where TDataTemplate : class
|
||||
public static Navigated<TTemplate, TContent> Create<TTemplate, TContent>(TTemplate template, TContent? content, IDictionary<string, object>? parameters = null) where TTemplate : class where TContent : class
|
||||
{
|
||||
return new Navigated<TTemplate, TDataTemplate>(content, dataContext, parameters);
|
||||
return new Navigated<TTemplate, TContent>(template, content, parameters);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,12 +8,12 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="JsonPatch.Net" Version="2.0.4" />
|
||||
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.1.0-preview1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
|
||||
<PackageReference Include="Mediator.Abstractions" Version="2.1.0-preview.5" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user