public async override Task Activate(INavigationService navigationService, NavigationContextBase navigationContext, IReadOnlyDictionary<string, object> pageState) { await base.Activate(navigationService, navigationContext, pageState); MyNavigationContext context = navigationContext as MyNavigationContext; if (pageState != null) { /********************************************************* * If you store state on deactivation and it is provided * here, you know that the user has used the back button * or returned from app suspension and should restore state. *********************************************************/ this.Value1 = pageState[nameof(this.Value1)] as string; this.Value2 = pageState[nameof(this.Value2)] as string; this.Value3 = pageState[nameof(this.Value3)] as string; } else if (context != null) { /********************************************************* * Alternatively, if the user has navigated to the page * for the first time, there won't be existing page state * to restore. The context can be used to populate the page. *********************************************************/ this.Value1 = context.Value1; this.Value2 = context.Value2; this.Value3 = context.Value3; } }
/// <summary> /// Adds a navigation context for storage. /// </summary> /// <param name="context">The context to be stored.</param> /// <returns>The ID of the context for later retrieval.</returns> public long Add(NavigationContextBase context) { if (context == null) { throw new ArgumentNullException("context"); } long id = this.CurrentId; this.contextStore.Add(id, context); return id; }
public async override Task Activate(INavigationService navigationService, NavigationContextBase navigationContext, IReadOnlyDictionary<string, object> pageState) { await base.Activate(navigationService, navigationContext, pageState); MyNavigationContext context = navigationContext as MyNavigationContext; if (pageState != null) { this.Value1 = pageState[nameof(this.Value1)] as string; this.Value2 = pageState[nameof(this.Value2)] as string; this.Value3 = pageState[nameof(this.Value3)] as string; } else if (context != null) { this.Value1 = context.Value1; this.Value2 = context.Value2; this.Value3 = context.Value3; } }
/// <summary> /// Navigate to the specified page with the provided context. /// </summary> /// <param name="pageType">The type of page to navigate to.</param> /// <param name="context">The context that should be provided to the page after navigation.</param> public void Navigate(Type pageType, NavigationContextBase context = null) { if (pageType == null) { throw new ArgumentNullException(nameof(pageType)); } if (context != null && !SuspensionManager.KnownTypes.Contains(context.GetType())) { throw new ArgumentException( "Cannot navigate with a context that has not been registered as a Known Type. Use NavigationService.AddKnownTypes to register this context type before use.", "context"); } object parameter = null; if (context != null) { parameter = this.contextService.Add(context); } this.RootFrame.Navigate(pageType, parameter); }
/// <summary> /// Creates a new NavigationService for a Frame, registering the frame and NavigationService /// for future retrieval and registering the frame with the NavigationContextService. /// </summary> /// <param name="frame">The frame to register.</param> /// <param name="defaultPage">The default page to navigate to if there is no state to restore.</param> /// <param name="defaultNavigationContext">The navigation context to provide on navigation to defaultPage.</param> /// <param name="restoreState">True to attempt to restore previously saved state, false to just navigate to defaultPage /// without attempting to restore state.</param> /// <returns>The NavigationService for the registered frame.</returns> public static INavigationService RegisterFrame(Frame frame, Type defaultPage, NavigationContextBase defaultNavigationContext = null, bool restoreState = true) { if (frame == null) { throw new ArgumentNullException(nameof(frame)); } if (defaultPage == null) { throw new ArgumentNullException(nameof(defaultPage)); } if (GetNavigationService(frame) != null) { throw new InvalidOperationException("Frame already associated with a NavigationService."); } // Clean up references before adding a new one. RemoveFrame(); NavigationService navigationService = new NavigationService(frame, new NavigationContextService()); // Ensure navigationService is fully registered before restoring state // or navigating so that it can be accessed by PageBase. NavigationServices.Add(navigationService); if (restoreState) { // This triggers NavigatedTo on PageBase if there is state restored. navigationService.RestoreState(); } // If state wasn't restored, navigate to a default page to populate the Frame. if (navigationService.RootFrame.Content == null) { navigationService.Navigate(defaultPage, defaultNavigationContext); } return navigationService; }