Quick Windows Phone 7 tip: navigating from page to page

Posted: (EET/GMT+2)

 

Lately, I've been focusing on developing applications for the Windows Phone 7. One of the very fundamental features of Windows Phone 7 applications (those written with Silverlight at least) is navigation between pages. However, if you are starting from scratch, then finding the correct way to move from one page to another can be difficult.

Even so, the solution is simple: just use the Navigate method of the NavigationService class. For instance, if you are currently in MainPage.xaml, and want to move to AnotherPage.xaml, then use the following code in C#:

NavigationService.Navigate(new Uri("/AnotherPage.xaml", UriKind.Relative));

Although this is easy, it is not very convenient to type. To help yourself, write for example the following C# helper method:

internal static void NavigateTo(this NavigationService service, string page)
{
  service.Navigate(new Uri("/" + page + ".xaml", UriKind.Relative));
}

With this method in place (use a static class as the container for this method), you can simply say:

NavigationService.NavigateTo("AnotherPage");

Good luck!