diff --git a/samples/mavvmApp/ViewModels/MainPageViewModel.cs b/samples/mavvmApp/ViewModels/MainPageViewModel.cs index 3f7337f..6fe23f4 100644 --- a/samples/mavvmApp/ViewModels/MainPageViewModel.cs +++ b/samples/mavvmApp/ViewModels/MainPageViewModel.cs @@ -48,7 +48,7 @@ void CountUp() async void Navigate() { - await BaseMethods.GoToViewModel(parameters: new NavigationParameters{ { "countParam", Count } }); + await BaseMethods.GoToViewModel(intermediates: new []{typeof(SecondPageViewModel), typeof(ThirdPageViewModel)}, new NavigationParameters{ { "countParam", Count } }); } } } diff --git a/samples/mavvmApp/ViewModels/SecondPageViewModel.cs b/samples/mavvmApp/ViewModels/SecondPageViewModel.cs index c3d122a..bbfa5cd 100644 --- a/samples/mavvmApp/ViewModels/SecondPageViewModel.cs +++ b/samples/mavvmApp/ViewModels/SecondPageViewModel.cs @@ -4,9 +4,11 @@ using Microsoft.Maui.Hosting; using mavvm; using CommunityToolkit.Mvvm.Input; +using mavvm.Attibutes; namespace mavvmApp.ViewModels { + [SectionRoute("main")] [QueryProperty(nameof(Count), "countParam")] public partial class SecondPageViewModel : BindableBase, IPageAware, INavigationAware { diff --git a/samples/mavvmApp/Views/LastPage.xaml b/samples/mavvmApp/Views/LastPage.xaml index 95f49c2..c8c84e9 100644 --- a/samples/mavvmApp/Views/LastPage.xaml +++ b/samples/mavvmApp/Views/LastPage.xaml @@ -4,7 +4,7 @@ x:Class="mavvmApp.Views.LastPage" Title="{Binding Title}" BackgroundColor="{DynamicResource SecondaryColor}" - Shell.PresentationMode="Animated"> + Shell.PresentationMode="ModalAnimated"> diff --git a/samples/mavvmApp/mavvmApp.csproj b/samples/mavvmApp/mavvmApp.csproj index 53d8d5d..353ec45 100644 --- a/samples/mavvmApp/mavvmApp.csproj +++ b/samples/mavvmApp/mavvmApp.csproj @@ -60,8 +60,8 @@ - - + + diff --git a/src/mavvm/Navigation/BaseMethods.cs b/src/mavvm/Navigation/BaseMethods.cs index 7aaea5f..5b4a1ec 100644 --- a/src/mavvm/Navigation/BaseMethods.cs +++ b/src/mavvm/Navigation/BaseMethods.cs @@ -30,7 +30,7 @@ public static async Task GoBack(bool replaceStack = false, NavigationParameters } } - public static async Task GoToViewModel(bool replaceStack = false, NavigationParameters parameters = null) + public static async Task GoToViewModel(bool replaceStack, NavigationParameters parameters = null) { var vmType = typeof(TViewModel); var path = (replaceStack ? "//" : "") + vmType.Name; @@ -58,6 +58,105 @@ public static async Task GoToViewModel(bool replaceStack = false, Na } } + public static async Task GoToViewModel(NavigationParameters parameters = null) + { + var vmType = typeof(TViewModel); + var path = vmType.Name; + + SelectCorrectTab(vmType); + + //HACK: Shell doesn't immediately know about CurrentPage. + await Task.Delay(50); + + if (Shell.Current?.CurrentPage?.BindingContext?.GetType() != vmType) + { + if (parameters is null) + { + await Shell.Current.GoToAsync(path); + } + else + { + await Shell.Current.GoToAsync(path, parameters); + } + } + + if (Shell.Current?.CurrentPage?.BindingContext is INavigateToAware vm) + { + vm.NavigatedTo(parameters); + } + } + + public static async Task GoToViewModel(Type[] intermediates, NavigationParameters parameters = null) + { + var vmType = typeof(TViewModel); + + var path = ""; + + foreach (var vmStep in intermediates) + { + path += $"{vmStep.Name}/"; + } + + path += vmType.Name; + + SelectCorrectTab(intermediates.Length > 0 ? intermediates[0] : vmType); + + //HACK: Shell doesn't immediately know about CurrentPage. + await Task.Delay(50); + + if (Shell.Current?.CurrentPage?.BindingContext?.GetType() != vmType) + { + if (parameters is null) + { + await Shell.Current.GoToAsync(path); + } + else + { + await Shell.Current.GoToAsync(path, parameters); + } + } + + if (Shell.Current?.CurrentPage?.BindingContext is INavigateToAware vm) + { + vm.NavigatedTo(parameters); + } + } + public static async Task GoToViewModel(Type intermediate, NavigationParameters parameters = null) + { + var vmType = typeof(TViewModel); + + var path = ""; + + if (intermediate is not null) + { + path += $"{intermediate.Name}/"; + } + + path += vmType.Name; + + SelectCorrectTab(intermediate ?? vmType); + + //HACK: Shell doesn't immediately know about CurrentPage. + await Task.Delay(50); + + if (Shell.Current?.CurrentPage?.BindingContext?.GetType() != vmType) + { + if (parameters is null) + { + await Shell.Current.GoToAsync(path); + } + else + { + await Shell.Current.GoToAsync(path, parameters); + } + } + + if (Shell.Current?.CurrentPage?.BindingContext is INavigateToAware vm) + { + vm.NavigatedTo(parameters); + } + } + static void SelectCorrectTab(Type viewModelType) { if (Application.Current?.MainPage is IShellWithTabBar shell) diff --git a/src/mavvm/mavvm.csproj b/src/mavvm/mavvm.csproj index 45c3395..de672e5 100644 --- a/src/mavvm/mavvm.csproj +++ b/src/mavvm/mavvm.csproj @@ -12,7 +12,7 @@ 21.0 10.0.18362.0 mavvm - 1.0.4 + 1.0.5 Vincent Niehues Vincent Niehues 2021 Vincent Niehues @@ -41,7 +41,7 @@ It uses the Shell we all know from xamarin together with the all new .net maui w - - + +