/// <summary> /// Invoked when application execution is being suspended. Application state is saved /// without knowing whether the application will be terminated or resumed with the contents /// of memory still intact. /// </summary> /// <param name="sender">The source of the suspend request.</param> /// <param name="e">Details about the suspend request.</param> private async void OnSuspending(object sender, SuspendingEventArgs e) { IsSuspending = true; try { var deferral = e.SuspendingOperation.GetDeferral(); //Bootstrap inform navigation service that app is suspending. NavigationService.Suspending(); // Save application state await SessionStateService.SaveAsync(); deferral.Complete(); } finally { IsSuspending = false; } }
/// <summary> /// Initializes the Frame and its content. /// </summary> /// <param name="args">The <see cref="IActivatedEventArgs"/> instance containing the event data.</param> /// <returns>A task of a Frame that holds the app content.</returns> protected 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(); if (ExtendedSplashScreenFactory != null) { Page extendedSplashScreen = this.ExtendedSplashScreenFactory.Invoke(args.SplashScreen); rootFrame.Content = extendedSplashScreen; } var frameFacade = new FrameFacadeAdapter(rootFrame); //Initialize MvvmAppBase common services SessionStateService = new SessionStateService(); //Configure VisualStateAwarePage with the ability to get the session state for its frame VisualStateAwarePage.GetSessionStateForFrame = frame => SessionStateService.GetSessionStateForFrame(frameFacade); //Associate the frame with a key SessionStateService.RegisterFrame(frameFacade, "AppFrame"); NavigationService = CreateNavigationService(frameFacade, SessionStateService); SettingsPane.GetForCurrentView().CommandsRequested += OnCommandsRequested; // 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 SessionStateService.RestoreSessionStateAsync(); } OnInitialize(args); if (args.PreviousExecutionState == ApplicationExecutionState.Terminated) { // Restore the saved session state and navigate to the last page visited try { SessionStateService.RestoreFrameState(); NavigationService.RestoreSavedNavigation(); _isRestoringFromTermination = true; } catch (SessionStateServiceException) { // 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); }