public void PopTo <T>() where T : class, IViewModel
        {
            var pagesToRemove = new List <Page>();
            var upper         = FormsNavigation.NavigationStack.Count;

            // Loop through the nav stack backwards
            for (int i = upper - 1; i >= 0; i--)
            {
                var currentPage = FormsNavigation.NavigationStack[i] as IViewFor;

                // Stop the whole show if one of the pages isn't an IViewFor
                if (currentPage == null)
                {
                    return;
                }

                var strongTypedPaged = currentPage as IViewFor <T>;

                // If we hit the view model type, break out
                if (strongTypedPaged != null)
                {
                    break;
                }

                // Finally - always add to the list
                pagesToRemove.Add(currentPage as Page);
            }

            foreach (var item in pagesToRemove)
            {
                FormsNavigation.RemovePage(item);
            }
        }
Esempio n. 2
0
        /// <inheritdoc />
        public async Task PushAsync(BaseNavigationViewModel viewModel, bool discardCurrent = false)
        {
            var currentPage = FormsNavigation.NavigationStack.LastOrDefault();
            var view        = InstantiateView(viewModel);

            if (currentPage.BindingContext is INavigationAware navPrevious)
            {
                navPrevious.OnNavigatingFrom();
            }

            if (viewModel is INavigationAware navForward)
            {
                navForward.OnNavigatingTo();
            }

            await FormsNavigation.PushAsync((Page)view);

            if (viewModel is INavigationAware navigated)
            {
                navigated.OnNavigatedTo();
            }

            if (discardCurrent)
            {
                FormsNavigation.RemovePage(currentPage);
            }
        }
        public async Task ReplaceAsync(Type pageModelType, NavigationState navState)
        {
            var oldTopPage = TopPage;

            await PushAsync(pageModelType, navState);

            FormsNavigation.RemovePage(oldTopPage);

            PrintNavigationStackDepth("ReplaceAsync");
        }
Esempio n. 4
0
        /// <inheritdoc />
        public async Task PushAsync(BaseNavigationViewModel viewModel, bool discardCurrent = false)
        {
            var currentPage = FormsNavigation.NavigationStack.LastOrDefault();
            var view        = InstantiateView(viewModel);
            await FormsNavigation.PushAsync((Page)view);

            if (discardCurrent && currentPage != null)
            {
                FormsNavigation.RemovePage(currentPage);
            }
        }
        public async Task ReplaceRootAsync(Type pageModelType,
                                           NavigationState navState = null)
        {
            var pageList = FormsNavigation.NavigationStack.ToList();

            await PushAsync(pageModelType, navState);

            // Clean up old pages
            foreach (var page in pageList)
            {
                FormsNavigation.RemovePage(page);
            }

            PrintNavigationStackDepth("ReplaceRootAsync");
        }
Esempio n. 6
0
        /// <inheritdoc />
        public async Task PushAsync <T>(Action <T> initialize = null, bool discardCurrent = false) where T : BaseNavigationViewModel
        {
            T viewModel;

            var currentPage = FormsNavigation.NavigationStack.LastOrDefault();

            // Instantiate the view model & invoke the initialize method, if any
            viewModel = Activator.CreateInstance <T>();
            initialize?.Invoke(viewModel);

            await PushAsync(viewModel);

            if (discardCurrent && currentPage != null)
            {
                FormsNavigation.RemovePage(currentPage);
            }
        }
Esempio n. 7
0
        public void RemovePage(NavigationViewModel viewModel)
        {
            var page = FormsNavigation.NavigationStack.FirstOrDefault(x => x.BindingContext == viewModel);

            FormsNavigation.RemovePage(page);
        }