User Tools

Site Tools


notes:uwp:navigation

Navigation in UWP

The Page class provides a set of virtual methods that can be used to implement application logic when the page is being navigated and when the user is navigating away from the page. These methods are useful for initializing the page or performing operations to save data when the user navigates away from it:

  • OnNavigatedFrom - invoked when the user has navigated away from the page and the page is no longer visible in the screen.
  • OnNavigatingFrom - invoked when the user has initiated an action to navigate away from the page, but the page is still visible in the screen; the OnNavigatingFrom method can be used to cancel the navigation by setting the Cancel property of the NavigatingCancelEventArgs instance to true.
  • OnNavigatedTo - invoked when the page is loaded and is visible in the screen.
Frame Page1 Page2
Frame.Navigate invoked in OnLaunched of application
Navigating
Navigated
NavigatedTo
The user navigates from Page1 to Page2
Navigating
NavigatingFrom
Ctor
Navigated (if navigation successful)
-or-
NavigationFailed (if navigation failed)
NavigatedFrom
NavigatedTo
Loaded
Unloaded

Architect your Page derivatives so they attach event handlers and obtain resources during OnNavigatedTo or Loaded and detach the handlers and release resources during OnNavigatedFrom or Unloaded.

Example: Override the OnNavigatedFrom method:

protected override void OnNavigatedFrom(NavigationEventArgs args)
{
    // Check the type of the page we are navigating from.
    if (args.SourcePageType == typeof(MyPage))
    {
        // ...
    }
 
    base.OnNavigatedFrom(args);
}

Example: Create a page (RootHost) that serves as a proxy to a frame object. It gives you the ability to overlay content on top of any of the windows in your application.

<Page x:Class="WBS.TestApp.RootHostPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
      <Frame x:Name="RootFrame" />
</Page>
namespace WBS.TestApp
{
    public sealed partial class RootHostPage : Page
    {
        public RootHostPage()
        {
            this.InitializeComponent();
 
            SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility =
                AppViewBackButtonVisibility.Visible;
            SystemNavigationManager.GetForCurrentView().BackRequested += RootHostPage_BackRequested;
 
            this.Loaded += RootHostPage_Loaded;
        }
 
        private void RootHostPage_Loaded(object sender, RoutedEventArgs args)
        {
            RootFrame.Navigate(typeof(MyPage2)); // MyPage2 is another page in the app
        }
 
        private void RootHostPage_BackRequested(object sender, BackRequestedEventArgs args)
        {
            if (RootFrame.CanGoBack)
            {
                args.Handled = true;
                RootFrame.GoBack();
            }
            else
            {
                args.Handled = false;
            }
        }
    }
}
notes/uwp/navigation.txt · Last modified: 2017/02/10 by admin