private async void ViewModel_NavigateToPage(object sender, AppNavigationEventArgs e) { InnerMenuGrid.FadeTo(0, 100); await MenuGrid.LayoutTo(new Rectangle(0, 0, Width, 0), 100, Easing.CubicOut); MenuGrid.FadeTo(0, 100); switch (e.Target) { case AppNavigation.Root: while (Navigation.NavigationStack.Count > 2) { Navigation.RemovePage(Navigation.NavigationStack[Navigation.NavigationStack.Count - 2]); } await Navigation.PopAsync(); break; case AppNavigation.Talents: await Navigation.PushAsync(new TalentsPage(ViewModel.SessionHandler)); break; case AppNavigation.NewDocument: await Navigation.PushAsync(new CreateDocumentPage(ViewModel.SessionHandler)); break; case AppNavigation.Documents: await Navigation.PushAsync(new DocumentsListPage(ViewModel.SessionHandler)); break; } }
/// <summary> /// Basic navigation implementation, you should only call it from a class that inherits from IViewModel /// </summary> /// <param name="sender">View model that triggered this event</param> /// <param name="navigationArgs">Arguments passed for navigation</param> private async void NavigateTo(object sender, AppNavigationEventArgs navigationArgs) { try { //Get the type of the view model var navigationType = navigationArgs.ViewModel.GetType(); //Search for the type in the registered pairs if (!_viewViewModelPairs.ContainsKey(navigationType)) { throw new ArgumentOutOfRangeException(nameof(navigationArgs.ViewModel)); } //If the triggering, make sure you call navigate from & unsubscribe from the event subscribed to below if (sender is IViewModel currentViewModel) { await currentViewModel.NavigateFrom(); currentViewModel.RequestNavigation -= NavigateTo; } //Remove any & all children from our NavigationGrid that we use for navigation NavigationGrid.Children.Clear(); //Get an existing view from the registered view - view model pairs var view = _viewViewModelPairs[navigationType]; //Set the data context of the view to the view model view.DataContext = navigationArgs.ViewModel; //Subscribe to the navigated event, this will be called when the view model wants to navigate to the next page navigationArgs.ViewModel.RequestNavigation += NavigateTo; //Add the view that we recieved as a child of the NavigationGrid NavigationGrid.Children.Add(view); //Call the navigate to, in order to initialize the view model await navigationArgs.ViewModel.NavigateTo(); } catch (Exception e) { Console.WriteLine($"Unhandled exception occured: {e.Message}"); } }