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
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
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
Looking at our Code Now that you have an overview of how
everything works, let's look at our code in greater
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.
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
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 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.
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: