diff --git a/Net.Maui.Extensions/ControlFlow/INavigationService.cs b/Net.Maui.Extensions/ControlFlow/INavigationService.cs index 17d84aa..dcfc2bc 100644 --- a/Net.Maui.Extensions/ControlFlow/INavigationService.cs +++ b/Net.Maui.Extensions/ControlFlow/INavigationService.cs @@ -1,11 +1,14 @@ -namespace Net.Maui.Extensions.ControlFlow; +using System.ComponentModel; + +namespace Net.Maui.Extensions.ControlFlow; public interface INavigationService { ContentPage? GetCurrent(); - T GoTo() - where T : ContentPage; + TPageType GoTo(TViewModelType? viewModel = default) + where TPageType : ContentPage + where TViewModelType : INotifyPropertyChanged, new(); void GoBack(); diff --git a/Net.Maui.Extensions/ControlFlow/NavigationService.cs b/Net.Maui.Extensions/ControlFlow/NavigationService.cs index 7659bd5..886ab75 100644 --- a/Net.Maui.Extensions/ControlFlow/NavigationService.cs +++ b/Net.Maui.Extensions/ControlFlow/NavigationService.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.Logging; +using System.ComponentModel; using System.Core.Extensions; using System.Extensions; using System.Extensions.Core; @@ -52,15 +53,17 @@ internal sealed class NavigationService : INavigationService this.ShowCurrentPage(); } - public T GoTo() where T : ContentPage + public TPageType GoTo(TViewModelType? viewModel = default) + where TPageType : ContentPage + where TViewModelType : INotifyPropertyChanged, new() { var scopedLogger = this.logger.CreateScopedLogger(); - scopedLogger.LogDebug($"Going to {typeof(T).Name}"); + scopedLogger.LogDebug($"Going to {typeof(TPageType).Name}"); - var context = this.GetScopedPageAndProvider(); + var context = this.GetScopedPageAndProvider(viewModel ?? new TViewModelType()); this.navigationStack.Push(context); this.ShowCurrentPage(); - return context.Page?.ThrowIfNull().Cast().ThrowIfNull()!; + return context.Page?.ThrowIfNull().Cast().ThrowIfNull()!; } public ContentPage? GetCurrent() @@ -91,17 +94,20 @@ internal sealed class NavigationService : INavigationService return; } - if (this.navigationStack.TryPeek(out var currentContext)) + if (this.navigationStack.TryPeek(out var currentContext) && + currentContext.Page is not null) { + currentContext.Page.BindingContext = currentContext.ViewModel; this.extendedApplication.MainPage = currentContext.Page; } } - private ScopedPageContext GetScopedPageAndProvider() - where T : ContentPage + private ScopedPageContext GetScopedPageAndProvider(TViewModelType viewModel) + where TPageType : ContentPage + where TViewModelType : INotifyPropertyChanged, new() { var scope = this.serviceProvider.CreateScope(); - return new ScopedPageContext { Page = scope.ServiceProvider.GetRequiredService(), Scope = scope }; + return new ScopedPageContext { Page = scope.ServiceProvider.GetRequiredService(), Scope = scope, ViewModel = viewModel }; } private ScopedPageContext GetScopedPageAndProvider(Type type) diff --git a/Net.Maui.Extensions/ControlFlow/ScopedPageContext.cs b/Net.Maui.Extensions/ControlFlow/ScopedPageContext.cs index f24c670..face70f 100644 --- a/Net.Maui.Extensions/ControlFlow/ScopedPageContext.cs +++ b/Net.Maui.Extensions/ControlFlow/ScopedPageContext.cs @@ -3,5 +3,6 @@ internal sealed class ScopedPageContext { public ContentPage? Page { get; init; } + public object? ViewModel { get; init; } public IServiceScope? Scope { get; init; } } diff --git a/Net.Maui.Extensions/Net.Maui.Extensions.csproj b/Net.Maui.Extensions/Net.Maui.Extensions.csproj index 3fdfefa..30bfeff 100644 --- a/Net.Maui.Extensions/Net.Maui.Extensions.csproj +++ b/Net.Maui.Extensions/Net.Maui.Extensions.csproj @@ -20,7 +20,7 @@ enable enable true - 0.2.6 + 0.2.7 Alexandru Macocian LICENSE true