×
Namespaces

Variants
Actions

How to pass strings between pages on Windows Phone

From Nokia Developer Wiki
Jump to: navigation, search


Overview

Use the Navigate() method of NavigationService to navigate between pages in a Windows Phone app and pass string parameters.

Note.pngNote: Page navigation is fully covered in How to perform page navigation on Windows Phone (Dev Center). This is a very brief overview of only one aspect.

Solution

NavigationService.Navigate(Uri) takes a Uri parameter, which can itself be formatted with string values/parameters. These values can be extracted from the NavigationContext in the receiving page.

Consider a simple app with 2 pages "page1", "page2".

To send a message (on click of submit button on "page1"):

private void submit_click(Object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Page2.xaml?msg="+ txtName.text, UriKind.Relative) );
}

To receive the parameter in page 2 we override onNavigatedTo(); the parameters can be retrieved using NavigationContext.QueryString.TryGetValue():

protected override void onNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.onNavigatedTo(e);
string msg;
if(NavigationContext.QueryString.TryGetValue(“msg”,out msg))
{
string name = msg;
}
}
WP Metro Icon UI.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
CompatibilityArticle
Created: SeemaB (25 Sep 2011)
Last edited: hamishwillee (26 Nov 2013)
This page was last modified on 26 November 2013, at 04:15.
485 page views in the last 30 days.
×