/// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property provides the group to be displayed.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Returning to a cached page through navigation shouldn't trigger state loading
            if (this._pageKey != null) return;

            var frameFacade = new FrameFacadeAdapter(this.Frame);
            var frameState = SuspensionManager.SessionStateForFrame(frameFacade);
            this._pageKey = "Page-" + frameFacade.BackStackDepth;

            if (e.NavigationMode == NavigationMode.New)
            {
                // Clear existing state for forward navigation when adding a new page to the
                // navigation stack
                var nextPageKey = this._pageKey;
                int nextPageIndex = frameFacade.BackStackDepth;
                while (frameState.Remove(nextPageKey))
                {
                    nextPageIndex++;
                    nextPageKey = "Page-" + nextPageIndex;
                }

                // Pass the navigation parameter to the new page
                this.LoadState(e.Parameter, null);
            }
            else
            {
                // Pass the navigation parameter and preserved page state to the page, using
                // the same strategy for loading suspended state and recreating pages discarded
                // from cache
                this.LoadState(e.Parameter, (Dictionary<String, Object>)frameState[this._pageKey]);
            }
        }
 /// <summary>
 /// Invoked when this page will no longer be displayed in a Frame.
 /// </summary>
 /// <param name="e">Event data that describes how this page was reached.  The Parameter
 /// property provides the group to be displayed.</param>
 protected override void OnNavigatedFrom(NavigationEventArgs e)
 {
     var frameFacade = new FrameFacadeAdapter(this.Frame);
     var frameState = SuspensionManager.SessionStateForFrame(frameFacade);
     var pageState = new Dictionary<String, Object>();
     this.SaveState(pageState);
     frameState[_pageKey] = pageState;
 }
Пример #3
0
        private async Task<Frame> InitializeFrameAsync(IActivatedEventArgs args)
        {
            var rootFrame = Window.Current.Content as Frame;
            // Do not repeat app initialization when the Window already has content,
            // just ensure that the window is active
            if (rootFrame == null)
            {
                // Create a Frame to act as the navigation context and navigate to the first page
                rootFrame = new Frame();
                var frameFacade = new FrameFacadeAdapter(rootFrame);

                //Associate the frame with a SuspensionManager key                                
                SuspensionManager.RegisterFrame(frameFacade, "AppFrame");

                //Initialize MvvmAppBase common services
                SuspensionManagerState = new SuspensionManagerState();
                NavigationService = CreateNavigationService(frameFacade, SuspensionManagerState);
                FlyoutService = new FlyoutService();
                FlyoutService.FlyoutResolver = CreateFlyoutView;
                // <snippet518>
                SettingsCharmService = new SettingsCharmService(GetSettingsCharmItems, FlyoutService);
                SettingsPane.GetForCurrentView().CommandsRequested += SettingsCharmService.OnCommandsRequested;
                // </snippet518>

                // Set a factory for the ViewModelLocator to use the default resolution mechanism to construct view models
                ViewModelLocator.SetDefaultViewModelFactory(Resolve);

                OnRegisterKnownTypesforSerialization();
                if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
                {
                    await SuspensionManager.RestoreSessionStateAsync();
                }

                OnInitialize(args);

                if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
                {
                    // Restore the saved session state and navigate to the last page visited
                    try
                    {
                        SuspensionManager.RestoreFrameState();
                        NavigationService.RestoreSavedNavigation();
                    }
                    catch (SuspensionManagerException)
                    {
                        // Something went wrong restoring state.
                        // Assume there is no state and continue
                    }
                }

                // Place the frame in the current Window
                Window.Current.Content = rootFrame;
            }

            return rootFrame;
        }