Passing Data Between Pages - Page 2
by kirupa |  20 August 2010

  Have questions? Discuss this Windows Phone tutorial with others on the forums.

In the previous page, you learned how to pass data from one page to another page when navigating. In this page, we'll learn how to read the data that you passed in earlier.

Reading the Data
To read data that has been passed in via a query string, there are two things you need to do:

  1. Overwrite a method called OnNavigatedTo
  2. Use NavigationContext.QueryString.TryGetValue to read the value given a key.

It would be easier if I explained all of this by just showing you the code that you will need to add. Open Results.xaml.cs and add the following method:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
 
string actorName = "";
 
bool actorValueExists = NavigationContext.QueryString.TryGetValue("actor", out actorName);
 
if (actorValueExists)
{
resultsField.Text = actorName;
}
}

Let's talk about the general approach before looking at what each line of code does. Behind the scenes, when you navigate between pages, a lot of methods are called internally and used by various parts of your application. One such method is OnNavigatedTo. This is one method that you never see unless you are really digging underground, but this method gets called every time your page is loaded as a result of a navigation action.

Because of when this method gets called, we can be assured that if data is passed in from the previous page, it will be accessible inside this method. That is why we override the original OnNavigatedTo method with our own and add our code to it. Once have overridden the method, all that is left is to read the data passed in.

You can read the data using the following line of code:

NavigationContext.QueryString.TryGetValue("key", out variable);

In your QueryString's TryGetValue method, you pass in the key whose value you are interested in. If the key is found, the value is returned to you by assigning the value to a variable you already have defined. The TryGetValue method itself only returns a boolean indicating whether its search was successful.

Looking at our Code
Now that you have an overview of how everything works, let's look at our code in greater detail:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
 
string actorName = "";
 
bool actorValueExists = NavigationContext.QueryString.TryGetValue("actor", out actorName);
 
if (actorValueExists)
{
resultsField.Text = actorName;
}
}

As described earllier, the first thing we do is override the OnNavigatedTo method. As you can infer from the method signature, this method is actually an event handler that responds to a NavigationEvent.


base.OnNavigatedTo(e);

In this line, because I am overriding the OnNavigatedTo method, I am calling the base the method's OnNavigatedTo method to make sure that me overriding this vital method doesn't result in important code that lives deep in the underworld not getting called.


string actorName = "";
 
bool actorValueExists = NavigationContext.QueryString.TryGetValue("actor", out actorName);

The next two lines pretty much go together. Here is where we are actually attempting to read the data that was passed in from the previous page. We are trying to read the value from the actor key. If the key exists, the entire TryGetValue call will return true, and the value of the actorName variable will be what gets returned.


if (actorValueExists)
{
resultsField.Text = actorName;
}

If the earlier TryGetValue call returned a true, then the actorValueExists variable will be set to true as well. This has the intended effect of causing the above if statement to evaluate to a true as well. When this if statement is true, our resultsField TextBlock will have its Text property set to the value of actorName which was retrieved delicately by TryGetValue.

Conclusion
So, there you have it. This page and the previous page talked about how to read data and how to pass data. As you can see, they are both fairly straightforward to do. Do note that there are other ways of sharing data between pages that doesn't involvie explicitly passing them. You could, for example, have variables defined at an application level in App.xaml and have those values be accessible everywhere.

Anyway, it seems like I forgot to write about how to be more like James Bond. That topic will be tabled for another tutorial. Getting back to this tutorial, below you will find the final source code for all of this:

Download Final Source Files

Got a question or just want to chat? Comment below or drop by our forums (they are actually the same thing!) where a bunch of the friendliest people you'll ever run into will be happy to help you out!

When Kirupa isn’t busy writing about himself in 3rd person, he is practicing social distancing…even on his Twitter, Facebook, and LinkedIn profiles.

Hit Subscribe to get cool tips, tricks, selfies, and more personally hand-delivered to your inbox.

COMMENTS

 1 | 2




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.