protected override void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); AppViewModel = e?.Parameter as AppLevelModel; AppViewModel.MessageToDisplay += AppViewModel_MessageToDisplay; AppViewModel.NavigateBack -= NavigateBack; AppViewModel.NavigateBack += NavigateBack; AppViewModel.NavigateTo += (page, viewModel, command, reentry) => { if (reentry || viewModel != AppViewModel?.ChildViewmodel) { viewModel.OnNavigatingTo(); MainPaneFrame.Navigate(page, viewModel); AppViewModel.VisitedPages.Push(viewModel); AppViewModel.ChildViewmodel = viewModel; AppViewModel.CanGoBack = !viewModel.IsInitialAppPage; ContentSplitView.IsPaneOpen = false; command?.Execute(); } }; }
/// <summary> /// Try to navigate Back /// </summary> /// <returns>True if operation successful</returns> private async Task <bool> TryNavagatingBack() { if (MainPaneFrame.CanGoBack && AppViewModel.VisitedPages.Count > 1) { // Get previous viewmodel var prevPage = AppViewModel.VisitedPages.Pop(); // Perform any cleanup before navigating away. if (await prevPage.OnNavigatingFrom()) { // We can navigate back. Perform cleanup and navigate back if (AppViewModel.ChildViewmodel != null) { AppViewModel.ChildViewmodel = AppViewModel.VisitedPages.Peek(); // Make sure any submenus are not unnecessarily selected if (AppViewModel.ChildViewmodel.ResetMainMenuIndexOnNavigation) { AppViewModel.ChildViewmodel.MainMenuIndex = -1; } if (AppViewModel.ChildViewmodel.ResetSubMenuIndexOnNavigation) { AppViewModel.ChildViewmodel.SubMenuIndex = -1; } if (AppViewModel.ChildViewmodel.ResetBottomMenuIndexOnNavigation) { AppViewModel.ChildViewmodel.BottomMenuIndex = -1; } } AppViewModel.CanGoBack = !AppViewModel.ChildViewmodel.IsInitialAppPage; // Go to previous page MainPaneFrame.GoBack(); return(true); } else { // Navigation cancelled. Restore navigation stack. AppViewModel.VisitedPages.Push(prevPage); return(false); } } else { AppViewModel.CanGoBack = false; return(false); } }
private async void NavigationHelper(MenuItem <AppLevelModel> item, int currentIndex) { if (item.UseBackButton) { await TryNavagatingBack(); } else { if (item.Reentry || item.ViewModel != AppViewModel?.ChildViewmodel) { bool canNavigate; if (AppViewModel.VisitedPages.Count == 0) { // No page to navigate away from canNavigate = true; } else { // Allow page to perform any cleanup if necessary. var currentPage = AppViewModel.VisitedPages.Peek(); canNavigate = await currentPage.OnNavigatingFrom(); } if (canNavigate) { item.BeforeNavigatingCommand?.Execute(); MainPaneFrame.Navigate(item.Page, item.ViewModel); AppViewModel.VisitedPages.Push(item.ViewModel); AppViewModel.ChildViewmodel = item.ViewModel; AppViewModel.CanGoBack = !item.ViewModel.IsInitialAppPage; item.AfterNavigatingCommand?.Execute(); } } } }