diff --git a/Mvvm.Nucleus.Maui/AppBuilderExtensions.cs b/Mvvm.Nucleus.Maui/AppBuilderExtensions.cs index eff1c94..9b90c98 100644 --- a/Mvvm.Nucleus.Maui/AppBuilderExtensions.cs +++ b/Mvvm.Nucleus.Maui/AppBuilderExtensions.cs @@ -33,6 +33,7 @@ public static MauiAppBuilder UseNucleusMvvm(this MauiAppBuilder bu builder.Services.TryAddSingleton(); builder.Services.TryAddTransient(); + builder.Services.TryAddSingleton(); builder.Services.AddSingleton(); builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); @@ -42,15 +43,8 @@ public static MauiAppBuilder UseNucleusMvvm(this MauiAppBuilder bu builder.Services.AddSingleton(serviceProvider => { var nucleusMvvmCore = serviceProvider.GetRequiredService(); - nucleusMvvmCore.Initialize(serviceProvider); - var nucleusMvvmOptions = serviceProvider.GetRequiredService(); - nucleusMvvmOptions.OnInitialized?.Invoke(serviceProvider); - - var shell = serviceProvider.GetRequiredService(); var application = nucleusMvvmCore.Application; - - application.MainPage = shell; application.PageAppearing += OnApplicationAppearing; return application; diff --git a/Mvvm.Nucleus.Maui/Mvvm.Nucleus.Maui.csproj b/Mvvm.Nucleus.Maui/Mvvm.Nucleus.Maui.csproj index a6efec3..d2c4bae 100644 --- a/Mvvm.Nucleus.Maui/Mvvm.Nucleus.Maui.csproj +++ b/Mvvm.Nucleus.Maui/Mvvm.Nucleus.Maui.csproj @@ -17,7 +17,7 @@ 10.0.17763.0 6.5 True - 0.3.0 + 0.3.1 Mvvm.Nucleus.Maui Nucleus MVVM for MAUI MVVM Framework for MAUI mobile applications. Build on top of MAUI and the MVVM Community Toolkit. @@ -34,8 +34,8 @@ - - + + diff --git a/Mvvm.Nucleus.Maui/NucleusMvvmCore.cs b/Mvvm.Nucleus.Maui/NucleusMvvmCore.cs index 242b0fb..0e5da75 100644 --- a/Mvvm.Nucleus.Maui/NucleusMvvmCore.cs +++ b/Mvvm.Nucleus.Maui/NucleusMvvmCore.cs @@ -17,6 +17,8 @@ public class NucleusMvvmCore internal NucleusMvvmOptions NucleusMvvmOptions { get; } + internal static bool IsInitialized => _current != null; + internal IDictionary NavigationParameters { get => _navigationParameters; @@ -70,8 +72,8 @@ public static NucleusMvvmCore Current /// public Shell? Shell { - get => _shell ?? throw new InvalidOperationException("NucleusMvvm could not detect a Shell. Set the MainPage to a Shell before using any Shell-related function, such as navigating."); - private set => RegisterShell(value); + get => _shell ?? throw new InvalidOperationException("NucleusMvvm could not detect a Shell."); + internal set => RegisterShell(value); } /// @@ -80,13 +82,18 @@ public Shell? Shell public Window? Window { get => _window ?? throw new InvalidOperationException("NucleusMvvm could not detect a Window."); - private set => RegisterWindow(value); + internal set => RegisterWindow(value); } + /// + /// Gets the current 'MainPage' from the . This is a read-only alternative to the deprecated 'Application.Current.MainPage'. + /// + public Page MainPage => _window?.Page ?? throw new InvalidOperationException("NucleusMvvm could not detect a MainPage."); + /// /// Gets the current . It takes into account modally presented pages, as well as pages like . /// - public Page CurrentPage => GetCurrentPage(Application.Current?.MainPage ?? throw new InvalidOperationException("NucleusMvvm could not detect the current page.")); + public Page CurrentPage => GetCurrentPage(MainPage ?? throw new InvalidOperationException("NucleusMvvm could not detect the current page.")); /// /// Initializes a new instance of the class. @@ -106,17 +113,7 @@ public NucleusMvvmCore(Application application, IViewFactory viewFactory, Nucleu internal void Initialize(IServiceProvider serviceProvider) { Current = this; - ServiceProvider = serviceProvider; - - Application.PropertyChanged += (sender, e) => - { - if (e.PropertyName == nameof(Application.MainPage)) - { - Shell = (sender as Application)?.MainPage as Shell; - Window = (sender as Application)?.MainPage?.Window; - } - }; } internal async void RunTaskInVoidAndTrackException(Func task, Action? onFinished = null, [CallerMemberName] string callerName = "") diff --git a/Mvvm.Nucleus.Maui/Services/NucleusWindowCreator.cs b/Mvvm.Nucleus.Maui/Services/NucleusWindowCreator.cs new file mode 100644 index 0000000..7626e85 --- /dev/null +++ b/Mvvm.Nucleus.Maui/Services/NucleusWindowCreator.cs @@ -0,0 +1,41 @@ +namespace Mvvm.Nucleus.Maui; + +/// +/// creates a that sets +/// as a MainPage. Register your own or child of this class for any +/// custom logic. +/// +public class NucleusWindowCreator : IWindowCreator +{ + private readonly IServiceProvider _serviceProvider; + + /// + /// Initializes a new instance of the class. + /// + /// The . + public NucleusWindowCreator(IServiceProvider serviceProvider) + { + _serviceProvider = serviceProvider; + } + + /// + public virtual Window CreateWindow(Application app, IActivationState? activationState) + { + if (!NucleusMvvmCore.IsInitialized) + { + var nucleusMvvmCore = _serviceProvider.GetRequiredService(); + var nucleusMvvmOptions = _serviceProvider.GetRequiredService(); + + nucleusMvvmCore.Initialize(_serviceProvider); + nucleusMvvmOptions.OnInitialized?.Invoke(_serviceProvider); + } + + var shell = _serviceProvider.GetRequiredService(); + var window = new Window(shell); + + NucleusMvvmCore.Current.Shell = shell; + NucleusMvvmCore.Current.Window = window; + + return window; + } +} \ No newline at end of file