コード例 #1
0
        public async Task NavigateToViewModelAsync <TViewModel>(
            object parameter     = null,
            bool modalNavigation = false,
            bool clearStack      = false,
            bool animated        = true)
            where TViewModel : ANavigableViewModel
        {
            if (clearStack)
            {
                var viewType = _viewLocator.GetViewTypeFor <TViewModel>();
                var rootPage = FormsNavigation.NavigationStack.First();
                if (viewType != rootPage.GetType())
                {
                    var newRootView = (Page)_viewLocator.GetViewFor <TViewModel>();

                    // Make the new view the root of our navigation stack
                    FormsNavigation.InsertPageBefore(newRootView, rootPage);
                    rootPage = newRootView;
                }

                // Then we want to go back to root page and clear the stack
                await NavigationPage.PopToRootAsync(animated);

                ((ANavigableViewModel)rootPage.BindingContext).OnNavigated(parameter);
                return;
            }

            var view = _viewLocator.GetViewFor <TViewModel>();

            if (modalNavigation)
            {
                await FormsNavigation.PushModalAsync((Page)view, animated);
            }
            else
            {
                await NavigationPage.PushAsync((Page)view, animated);
            }

            ((ANavigableViewModel)view.BindingContext).OnNavigated(parameter);
        }