App
class (App.xaml.cs) creates and sets the window's Frame in OnLaunched()
protected async override void OnLaunched(LaunchActivatedEventArgs e) { ... Frame 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(); ... // Place the frame in the current Window Window.Current.Content = rootFrame; }
MainPage
) is navigated to in OnLaunched()
rootFrame.Navigate(typeof(MainPage), e.Arguments);
Frame.Navigate()
to navigate to the new page
this.Frame.Navigate(typeof(SecondPage));
protected override void OnNavigatedTo(NavigationEventArgs args) { // Page was navigated to } protected override void OnNavigatedFrom(NavigationEventArgs args) { // Page is being navigated away from }
this.Frame.Navigate(typeof(SecondPage), "Message for SecondPage");
OnNavigatedTo
protected override void OnNavigatedTo(NavigationEventArgs e) { string message = e.Parameter as string; if (string.IsNullOrWhiteSpace(message)) myTextBox.Text = "Go back and give me a message!"; else myTextBox.Text = "Received " + message; }
this.Frame.GoBack(); // Go to previous page this.Frame.GoForward(); // Go to next page
OnLaunched
, add 1) Navigated handler, 2) BackRequested handler. Add both handlers as well.
protected override void OnLaunched(LaunchActivatedEventArgs e) { ... if (rootFrame == null) { rootFrame = new Frame(); // ADD: Register Navigated event handler rootFrame.Navigated += OnNavigated; ... // ADD: Register BackRequested event handler SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested; } ... } // ADD: Navigated event handler private void OnNavigated(object sender, NavigationEventArgs e) { // Determine if Back button should be visible SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = ((Frame)sender).CanGoBack ? AppViewBackButtonVisibility.Visible : AppViewBackButtonVisibility.Collapsed; } // ADD: BackRequested event handler private void OnBackRequested(object sender, BackRequestedEventArgs e) { Frame rootFrame = Window.Current.Content as Frame; if (rootFrame.CanGoBack) { e.Handled = true; rootFrame.GoBack(); } }
public MainPage() { this.InitializeComponent(); this.NavigationCacheMode = NavigationCacheMode.Enabled; }