HOW TO PASS MORE PARAMETERS IN PAGE NAVIGATION OF WINDOWS 8 PHONE
Parameters are most easily passed from one page to another by encoding them into the query string used to navigate to the target page. Below is code from an event handler that will navigate to the target with two parameter values :
private void passParameters_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Page2.xaml?msg1=" + textBox1.Text +
"&msg2=" + textBox2.Text,
UriKind.Relative));
}
The example happens to be a Click handler for a Button control, however, any similar code will do. The values in the example are provided by the contents of TextBox controls on the source page, but can come from any source of text.
The point is to format the URI to contain the parameter names and values that the target page is expecting. Parameters and values are encoded as name/value pairs in the form, “name=value” where each name/value pair is separated from its predecessor with an “&” character.
The point is to format the URI to contain the parameter names and values that the target page is expecting. Parameters and values are encoded as name/value pairs in the form, “name=value” where each name/value pair is separated from its predecessor with an “&” character.
The target page extracts the parameter values in the OnNavigatedTo event handler of the derived target PhoneApplicationPage. If the parameter value is optional, then it is the responsibility of the code in this function to handle the case where the parameter value may be either present or absent. Here is an example that matches the sample above:
protected override voidOnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string msg = string.Empty; // Assume no value provided
if (NavigationContext.QueryString.TryGetValue("msg1", out msg))
{
textBlock1.Text = msg; // Stores the value in a TextBlock
}
msg = string.Empty; // Assume no value provided
if (NavigationContext.QueryString.TryGetValue("msg2", out msg))
{
textBlock2.Text = msg; // Stores the value in a another TextBlock
}
}
}
The example above simply displays each parameter value in the target page; how you actually decide to process such values is application dependent.
No comments:
Post a Comment