Windows Phone Fundamentals–Navigation

Windows Phone Fundamentals

Continuing my mini-series on Windows Phone Fundamentals, begun here, this tutorial will Navigation cover the topic of navigating from one page to another.  This is one of those tasks that are wicked simple once you know how, but which are not at first obvious at all.

To see this at work, create a new Windows Phone application and name it navigation.  Open MainPage.xaml and set the ApplicationTitle to Navigation and set the PageTitle to Page 1.

Create a second page by right clicking on the project file and choosing Add –> New Item.  From the dialog box choose  Windows Phone Portrait Page and name it Page 2.xaml.  In the newly created page set the ApplicationTitle to Navigation and set the PageTitle to Page 2.

Switch back to page 1 (MainPage.xaml) and drag and drop a HyperlinkButton onto the page.  In the properties window find the Content property and change it to Page 2 and then set the NavigateUri property to /Page2.xaml. 

Switch to Page2.xaml and drag a button onto the page.  In the xaml set its content to Page 1 and set its name to Navigate.   Now let’s set up an event handler for this button’s click event.  Open Page2.xaml.cs and add the following to the constructor:

Navigate.Click += Navigate_Click;

In the stubbed out Navigate_Click method replace the exception with the following code:

NavigationService.Navigate( 
   new Uri( "/MainPage.xaml", UriKind.RelativeOrAbsolute ) );

Build and run your application, you should now be able to navigate back and forth between the two pages.

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 online courses. His latest book, Building APIs with .NET will be released early in 2025. Liberty is a Senior SW Engineer for CNH and 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 Microsoft MVP.
This entry was posted in Essentials, Mango, Mini-Tutorial, Patterns & Skills, WindowsPhone and tagged . Bookmark the permalink.

One Response to Windows Phone Fundamentals–Navigation

Comments are closed.