In Focus

Passing Parameters Among Pages in Windows Phone 8 Apps

This article covers a simple way to send data from one page to another in Windows Phone 8 Apps.

Problem

You have an application with more than one page. And you want to send values from one page to another.

Solution

Sometimes you need global variables that can store current user activities and such. The best thing to store and display a global value is doing it the "Querystring Way". Web Developers surely understand what I'm talking about. You can set external variables when you write navigation code:

private void button1_Click(object sender, RoutedEventArgs e)

{
   NavigationService.Navigate(new Uri("/Page2.xaml?ID=1", UriKind.Relative));
}

Just like in the web!

When we navigated to Page2.xaml, we must also display them, right? 

This simple code will display the value in a TextBlock control:

string var;
if(NavigationContext.QueryString.TryGetValue("ID",out var))
{
  textBlock1.Text = var;
}

 
Now let's see the results:

MainPage.xaml

 
ss4.png 
 
Page2.xaml

ss5.png
 
We've successfully passed variables from one page to another.