Windows Phone From Scratch – Navigation

To navigate from one page to another, you can take advantage of the static NavNavigate method on the page’s read-only NavigationService property.  This passes your request to the NavigationService Singleton for your application, and greatly simplifies the navigation syntax.

A Quick Demo

Demos should, in my opinion, be absolutely as simple as humanly possible, to keep the focus on the topic at hand.  In that spirit, we’ll create a new application with just three pages:

  • Main Page
  • Page1
  • Page2

    For each page we’ll adjust the application title and page title, so that the page is self-identifying; e.g.,

          Text="Phone Nav"
          Style="{StaticResource PhoneTextNormalStyle}" />
          Text="Page 2"
          Style="{StaticResource PhoneTextTitle1Style}" />

    On MainPage we’ll place two buttons, one saying Page 1 and the other saying Page 2.  We’ll also implement event handlers for each, that use the Navigation service.  Here is the complete code-behind for MainPage,

    using System;
    using System.Windows;
    using Microsoft.Phone.Controls;
    namespace PhoneNav
       public partial class MainPage : PhoneApplicationPage
          public MainPage()
             Page1.Click += Page1_Click;
             Page2.Click += Page2_Click;
          void Page2_Click( object sender, RoutedEventArgs e )
                new Uri( "/Page2.xaml", UriKind.Relative ) );
          void Page1_Click( object sender, RoutedEventArgs e )
                new Uri( "/Page1.xaml", UriKind.Relative ) );

    Run the application. Clicking Page1 takes you there instantly (we may want to add animation at some point to make the transition less confusing).

    For now, to navigate back, use the back button.

    In the next mini-tutorial we’ll look at the events that are raised during navigation and how you can override them to take greater control of the navigation.

    About Jesse Liberty

    Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen Pluralsight & LinkedIn Learning courses. He was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Xamarin Certified Mobile Developer and a Xamarin MVP and a Microsoft MVP.
    This entry was posted in Patterns & Skills. Bookmark the permalink.

    4 Responses to Windows Phone From Scratch – Navigation

    1. Pingback:

    Comments are closed.