예제 #1
0
        public async Task <bool> GoBack()
        {
            if (_isNavigating)
            {
                throw new InvalidOperationException("Navigation already in progress.");
            }

            _isNavigating    = true;
            this.CanNavigate = false;
            this.CanGoBack   = false;

            try
            {
                await this.WaitForLoadedAsync();

                AlternativePage currentPage = null;
                var             currentJe   = this.BackStack.Pop();
                var             je          = BackStack.Peek();

                if (_currentPagePresenter != null)
                {
                    currentPage = (AlternativePage)_currentPagePresenter.Content;
                    var cancelArgs =
                        new AlternativeNavigatingCancelEventArgs(
                            NavigationMode.Back,
                            je.Type);
                    await currentPage.OnNavigatingFromInternal(cancelArgs);

                    if (cancelArgs.Cancel)
                    {
                        this.BackStack.Push(currentJe);
                        return(false);
                    }
                }

                AlternativePage  newPage;
                ContentPresenter newPagePresenter;

                if (_preloadedPageCache.ContainsKey(je))
                {
                    newPagePresenter = _preloadedPageCache[je];
                    newPage          = (AlternativePage)newPagePresenter.Content;
                    _preloadedPageCache.Remove(je);
                }
                else
                {
                    newPage          = (AlternativePage)Activator.CreateInstance(je.Type);
                    newPage.Frame    = this;
                    newPagePresenter = new ContentPresenter {
                        Style = PagePresenterStyle
                    };
                    newPagePresenter.Content = newPage;
                    _pagePresentersPanel.Children.Add(newPagePresenter);
                }

                newPagePresenter.Opacity = 0.005;

                await UnloadPreloadedPages();

                var args = new AlternativeNavigationEventArgs(
                    newPage.Content, NavigationMode.New, je.Parameter, je.Type);
                await newPage.OnNavigatingToInternal(args);

                await newPagePresenter.WaitForLoadedAsync();

                await newPagePresenter.WaitForNonZeroSizeAsync();

                newPagePresenter.Opacity = 1.0;

                if (this.PageTransition != null)
                {
                    await this.PageTransition.TransitionBackward(_currentPagePresenter, newPagePresenter);
                }

                if (currentPage != null)
                {
                    await currentPage.OnNavigatedFromInternal(args);

                    _pagePresentersPanel.Children.Remove(_currentPagePresenter);
                }

                _currentPagePresenter = newPagePresenter;

                await newPage.OnNavigatedToInternal(args);

                return(true);
            }
            finally
            {
                _isNavigating    = false;
                this.CanNavigate = true;
                this.CanGoBack   = this.BackStack.Count > 1;
            }
        }