This entry is just a follow-up on my last entry. I was writing about navigating and passing data between XAML pages.

So, for this entry I am going to use the same project I created in my last entry.

Each time I am navigating to a new XAML page I am creating a new instance of that class that is represented by the xaml and the C# code behind file. In my example, when I open up a new page, I am creating a new instance of the class called Page1.

Assume I have an application with a TextBox, a Button and a HyperlinkButton. What if a user enters some information in the TextBox and then clicks on the HyperlinkButton? In this situation, the application navigates to another page. That page might also contain a HyperlinkButton, pointing back to the original page. The question now is what happened to the data the user entered in the TextBox on the original page?

Let’s see. I am adding a textbox to my MainPage.xaml’s designer surface and clear out the text property. Then on Page1.xaml I am adding a Hyperlink Button, and I am going to navigate this back to the Main Page:

I’ve typed in some information. Clicking on the Hyperlink Button navigates me to Page1. If I now use the Back key on the Emulator it brings me back to the Main Page, and I notice, my typed in information is still there:

If I click that new Hyperlink Button on Page1 to navigate back, my typed in information has disappeared:

So, when the user clicks the Back key, assuming the application hasn’t been terminated, the page’s state, or rather the properties that have been modified for the controls on that page are retained in the phone’s memory. When the user uses the Hyperlink Button to navigate back, a new instance of that page is created.

As a final thing in this example I go to my MainPage.xaml.cs file and add the following code right under the part where it says Constructor:

This piece of code basically is overriding the default functionality of the Phone Application Page, which is the top most page of my application. It is modifying the default behavior. And it does that by overriding the code in the OnNavigatedFrom and OnNavigatedTo Event Handlers. In other words, I want the code to do this: “pas.State[“myValue”] = textBox1.Text;” whenever OnNavigatedFrom or OnNavigatedTo is called. And once it is finished with doing that, I want it to execute “ base.OnNavigatedFrom(e);”. I want it to take what ever code I already had and execute it.

Generally I am just saying, I want to take control of what happens in my application right before the app navigates from the current page to a new page.

So, when the user is trying to navigate away from the MainPage.xaml I want to save the values that were in the textbox control into that pas (phone app service) State. Then if the user is navigating back to that page, the app checks if that key, my value exists, and if it does, then the app is going to retrieve the associated value to “someObject”. It’s trying to get that value (TryGetValue) and if it does, it is sending this someObject back to the textbox where it originally came from.


To be continued…

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: