Navigation and Passing Data Between WP7 Pages

Unlike other popular mobile platforms, Windows Phone 7 (WP7) uses a different scheme for navigation between screens. The first thing to point out is that WP7 screens are actually called Pages. Think of WP7 Pages as HTML pages within a web browser. While this may seem like a stretch, in actuality it isn't too far off. Similar to a web browser viewing a web site, you have a default page and you click on links to navigate to other pages, and you can back up to a previous page by clicking on the back button.

Since all WP7 devices come with a back button, the WP7 operating system needs a hierarchy of navigation to allow the user to go back to a previous page. HTML pages within a web browser support a back button because of the history created by the user following links and through redirects. Thus if the user clicks the back button the web browser returns to the previous page in history. While WP7 does not have the equivalent of a link, it does, however, have the equivalent of a redirect provided by the NavigationService.Navigate method. This method provides another similarity to a redirect in that it also uses an equivalent URL. Known as a URI (Universal Resource Identifier) it serves the same purpose of a path to the Page as well as a Query String. In fact the form is the same as you would expect in a traditional web site.

Next, we can dig into an example for using navigation within WP7 such as the list and detail screen. For instance, when we have a ListBox control on a Page and would like to send the user to a details page. The following code snippet shows the usage of the Navigate method to direct the user to the DetailsPage with a single query string parameter called selectedItem.

NavigationService.Navigate(new Uri("/DetailsPage.xaml?selectedItem=" + MainListBox.SelectedIndex, UriKind.Relative));

Thus when this method is called the user is sent directly to the DetailsPage. When the DetailsPage is displayed, you will then need to extract the Query String parameter to direct the user to the correct record. The following snippet shows the OnNavigateTo method, which is called when you use the NavigationService to navigate to a Page.

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
string selectedIndex = ""; 

if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex)) 
{ 
int index = int.Parse(selectedIndex); 

//Display the data for the selected index ... 
} 
}

As you can see, it is quite easy to pull parameters out from the Query String using the TryGetValue method from the NavigationContext.QueryString class.

Conclusion

With the above code snippets it should be easy to see the similarities between HTML Pages with regard to navigation and passing data between Pages. If you are familiar with creating sites using ASP.Net you should feel right at home with regard to navigation. While the above snippets were pulled from the Windows Phone DataBound Application template to support the List/Detail pages, you should be able to see how you can expand these snippets to support the navigation needs of your application.



About the Author

Chris Bennett

Chris Bennett is a Manager with Crowe Horwath LLP in the Indianapolis office. He can be reached at chris.bennett@crowehorwath.com.

Related Articles

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • You may already know about some of the benefits of Bluemix, IBM's open platform for developing and deploying mobile and web applications. Check out this webcast that focuses on building an Android application using the MobileData service, with a walk-through of the real process and workflow used to build and link the MobileData service within your application. Join IBM's subject matter experts as they show you the way to build a base application that will jumpstart you into building your own more complex app …

  • On-demand Event Event Date: September 17, 2014 Another day, another end-of-support deadline. You've heard enough about the hazards of not migrating to Windows Server 2008 or 2012. What you may not know is that there's plenty in it for you and your business, like increased automation and performance, time-saving technical features, and a lower total cost of ownership. Check out this webcast and join Rich Holmes, Pomeroy's practice director of virtualization, as he discusses the future state of your servers, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds