diff --git a/src/MauiMicroMvvm/Internals/DefaultNavigation.cs b/src/MauiMicroMvvm/Internals/DefaultNavigation.cs index 97faa04..c485e2c 100644 --- a/src/MauiMicroMvvm/Internals/DefaultNavigation.cs +++ b/src/MauiMicroMvvm/Internals/DefaultNavigation.cs @@ -5,22 +5,23 @@ namespace MauiMicroMvvm.Internals; [EditorBrowsable(EditorBrowsableState.Never)] public class DefaultNavigation : INavigation where TShell : Shell { - private readonly TShell _shell; + private readonly Lazy _lazyShell; + private TShell Shell => _lazyShell.Value; - public DefaultNavigation(TShell shell) + public DefaultNavigation(IServiceProvider services) { - _shell = shell; + _lazyShell = new Lazy(services.GetRequiredService); } public async Task GoToAsync(string uri) => - await _shell.GoToAsync(uri); + await Shell.GoToAsync(uri); public async Task GoToAsync(string uri, IDictionary parameters) => - await _shell.GoToAsync(uri, parameters); + await Shell.GoToAsync(uri, parameters); public async Task GoToAsync(string uri, bool animate) => - await _shell.GoToAsync(uri, animate); + await Shell.GoToAsync(uri, animate); public async Task GoToAsync(string uri, bool animate, IDictionary parameters) => - await _shell.GoToAsync(uri, animate, parameters); + await Shell.GoToAsync(uri, animate, parameters); } diff --git a/src/MauiMicroMvvm/Internals/PageDialogs.cs b/src/MauiMicroMvvm/Internals/PageDialogs.cs index 055d170..970304c 100644 --- a/src/MauiMicroMvvm/Internals/PageDialogs.cs +++ b/src/MauiMicroMvvm/Internals/PageDialogs.cs @@ -2,28 +2,29 @@ internal class PageDialogs : IPageDialogs where TShell : Shell { - private readonly TShell _shell; + private readonly Lazy _lazyShell; + private TShell Shell => _lazyShell.Value; - public PageDialogs(TShell shell) + public PageDialogs(IServiceProvider services) { - _shell = shell; + _lazyShell = new Lazy(services.GetRequiredService); } public Task DisplayActionSheet(string title, string cancel, string destruction, params string[] buttons) => - _shell.DisplayActionSheet(title, cancel, destruction, buttons); + Shell.DisplayActionSheet(title, cancel, destruction, buttons); public Task DisplayActionSheet(string title, string cancel, string destruction, FlowDirection flowDirection, params string[] buttons) => - _shell.DisplayActionSheet(title, cancel, destruction, flowDirection, buttons); + Shell.DisplayActionSheet(title, cancel, destruction, flowDirection, buttons); public Task DisplayAlert(string title, string message, string cancel) => - _shell.DisplayAlert(title, message, cancel); + Shell.DisplayAlert(title, message, cancel); public Task DisplayAlert(string title, string message, string cancel, FlowDirection flowDirection) => - _shell.DisplayAlert(title, message, cancel, flowDirection); + Shell.DisplayAlert(title, message, cancel, flowDirection); public Task DisplayAlert(string title, string message, string accept, string cancel) => - _shell.DisplayAlert(title, message, accept, cancel); + Shell.DisplayAlert(title, message, accept, cancel); public Task DisplayAlert(string title, string message, string accept, string cancel, FlowDirection flowDirection) => - _shell.DisplayAlert(title, message, accept, cancel, flowDirection); + Shell.DisplayAlert(title, message, accept, cancel, flowDirection); } diff --git a/src/MauiMicroMvvm/MauiMicroBuilderExtensions.cs b/src/MauiMicroMvvm/MauiMicroBuilderExtensions.cs index 3e1ab8a..44c3eda 100644 --- a/src/MauiMicroMvvm/MauiMicroBuilderExtensions.cs +++ b/src/MauiMicroMvvm/MauiMicroBuilderExtensions.cs @@ -94,17 +94,19 @@ public static IServiceCollection MapView(this IServiceCollect if (typeof(TView).IsAssignableTo(typeof(Page))) Routing.RegisterRoute(key, typeof(TView)); - return services.AddTransient(sp => - { - var viewFactory = sp.GetRequiredService(); - var view = viewFactory.CreateView(); - viewFactory.Configure(view); - return view; - }) + return (typeof(TView).IsAssignableTo(typeof(Shell)) ? services.AddSingleton(CreateView) : services.AddTransient(CreateView)) .AddSingleton(new ViewMapping(key, typeof(TView), typeof(TViewModel))) .AddTransient(); } + private static TView CreateView(IServiceProvider sp) where TView : VisualElement + { + var viewFactory = sp.GetRequiredService(); + var view = viewFactory.CreateView(); + viewFactory.Configure(view); + return view; + } + public static IServiceCollection ApplyBehavior(this IServiceCollection services) where TView : VisualElement where TBehavior : Behavior