Add support for self scope
This commit is contained in:
@@ -29,13 +29,12 @@ public class ComponentBuilder :
|
|||||||
|
|
||||||
services.AddScoped<SubscriptionCollection>();
|
services.AddScoped<SubscriptionCollection>();
|
||||||
services.AddScoped<ISubscriptionManager, SubscriptionManager>();
|
services.AddScoped<ISubscriptionManager, SubscriptionManager>();
|
||||||
|
services.AddScoped<IDisposer, Disposer>();
|
||||||
|
|
||||||
services.AddTransient<ISubscriber, Subscriber>();
|
services.AddTransient<ISubscriber, Subscriber>();
|
||||||
services.AddTransient<IPublisher, Publisher>();
|
services.AddTransient<IPublisher, Publisher>();
|
||||||
|
|
||||||
services.AddTransient<IMediator, Mediator>();
|
services.AddTransient<IMediator, Mediator>();
|
||||||
services.AddScoped<IDisposer, Disposer>();
|
|
||||||
|
|
||||||
services.AddTransient<INavigationScope, NavigationScope>();
|
services.AddTransient<INavigationScope, NavigationScope>();
|
||||||
|
|
||||||
services.AddTransient<INavigationProvider, NavigationProvider>();
|
services.AddTransient<INavigationProvider, NavigationProvider>();
|
||||||
|
|||||||
@@ -24,11 +24,12 @@ public class DefaultHostBuilder :
|
|||||||
ComponentHostCollection>();
|
ComponentHostCollection>();
|
||||||
|
|
||||||
services.AddScoped<SubscriptionCollection>();
|
services.AddScoped<SubscriptionCollection>();
|
||||||
services.AddScoped<ISubscriptionManager, SubscriptionManager>();
|
|
||||||
services.AddTransient<ISubscriber, Subscriber>();
|
|
||||||
services.AddTransient<IPublisher, Publisher>();
|
|
||||||
|
|
||||||
services.AddScoped<IMediator, Mediator>();
|
services.AddTransient<ISubscriptionManager, SubscriptionManager>();
|
||||||
|
services.AddTransient<ISubscriber, Subscriber>();
|
||||||
|
|
||||||
|
services.AddTransient<IPublisher, Publisher>();
|
||||||
|
services.AddTransient<IMediator, Mediator>();
|
||||||
|
|
||||||
services.AddScoped<IProxyService<IPublisher>>(provider =>
|
services.AddScoped<IProxyService<IPublisher>>(provider =>
|
||||||
new ProxyService<IPublisher>(provider.GetRequiredService<IPublisher>()));
|
new ProxyService<IPublisher>(provider.GetRequiredService<IPublisher>()));
|
||||||
@@ -48,7 +49,7 @@ public class DefaultHostBuilder :
|
|||||||
services.AddScoped<INavigationRegionCollection, NavigationRegionCollection>();
|
services.AddScoped<INavigationRegionCollection, NavigationRegionCollection>();
|
||||||
services.AddTransient<INavigationRegionProvider, NavigationRegionProvider>();
|
services.AddTransient<INavigationRegionProvider, NavigationRegionProvider>();
|
||||||
|
|
||||||
services.AddTransient<INavigationScope, NavigationScope>();
|
services.AddScoped<INavigationScope, NavigationScope>();
|
||||||
|
|
||||||
services.AddSingleton(new NamedComponent("Root"));
|
services.AddSingleton(new NamedComponent("Root"));
|
||||||
services.AddScoped<IComponentScopeCollection, ComponentScopeCollection>(provider => new ComponentScopeCollection
|
services.AddScoped<IComponentScopeCollection, ComponentScopeCollection>(provider => new ComponentScopeCollection
|
||||||
|
|||||||
@@ -3,20 +3,28 @@
|
|||||||
namespace Toolkit.Foundation;
|
namespace Toolkit.Foundation;
|
||||||
|
|
||||||
public class NavigateHandler(NamedComponent scope,
|
public class NavigateHandler(NamedComponent scope,
|
||||||
IComponentScopeProvider provider) :
|
IComponentScopeProvider componentScopeProvider,
|
||||||
|
IServiceProvider provider) :
|
||||||
INotificationHandler<Navigate>
|
INotificationHandler<Navigate>
|
||||||
{
|
{
|
||||||
public async Task Handle(Navigate args,
|
public async Task Handle(Navigate args,
|
||||||
CancellationToken cancellationToken)
|
CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
if (provider.Get(args.Scope ?? scope.Name)
|
INavigationScope? navigationScope;
|
||||||
is ComponentScopeDescriptor descriptor)
|
if (args.Scope == "self")
|
||||||
{
|
{
|
||||||
if (descriptor?.Services?.GetService<INavigationScope>() is INavigationScope navigationScope)
|
navigationScope = provider.GetRequiredService<INavigationScope>();
|
||||||
{
|
}
|
||||||
await navigationScope.NavigateAsync(args.Route, args.Sender,
|
else
|
||||||
args.Context, args.Navigated, args.Parameters, cancellationToken);
|
{
|
||||||
}
|
ComponentScopeDescriptor? descriptor = componentScopeProvider.Get(args.Scope ?? scope.Name);
|
||||||
|
navigationScope = descriptor?.Services?.GetRequiredService<INavigationScope>();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (navigationScope is not null)
|
||||||
|
{
|
||||||
|
await navigationScope.NavigateAsync(args.Route, args.Sender,
|
||||||
|
args.Context, args.Navigated, args.Parameters, cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using CommunityToolkit.Mvvm.ComponentModel;
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using System.Collections.Specialized;
|
using System.Collections.Specialized;
|
||||||
@@ -140,11 +141,18 @@ public partial class ObservableCollectionViewModel<TViewModel> :
|
|||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TViewModel Add<T>()
|
public TViewModel Add<T>(bool scope = false)
|
||||||
where T :
|
where T :
|
||||||
TViewModel
|
TViewModel
|
||||||
{
|
{
|
||||||
T? item = Factory.Create<T>();
|
IServiceFactory? factory = null;
|
||||||
|
if (scope)
|
||||||
|
{
|
||||||
|
IServiceScope serviceScope = Provider.CreateScope();
|
||||||
|
factory = serviceScope.ServiceProvider.GetRequiredService<IServiceFactory>();
|
||||||
|
}
|
||||||
|
|
||||||
|
T? item = factory is not null ? factory.Create<T>() : Factory.Create<T>();
|
||||||
Add(item);
|
Add(item);
|
||||||
|
|
||||||
return item;
|
return item;
|
||||||
|
|||||||
Reference in New Issue
Block a user