Over a million developers have joined DZone.

Windows Phone 'TryGetValue' Between XAML Pages

· Mobile Zone

Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud, brought to you in partnership with IBM.

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 is disappeared:

2     4


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:

    PhoneApplicationService pas = PhoneApplicationService.Current;
    protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        pas.State["myValue"] = textBox1.Text;
    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        object someObject;
        if (pas.State.ContainsKey("myValue"))
            if (pas.State.TryGetValue("myValue", out someObject))
                textBox1.Text = someObject.ToString();

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…

Source: http://andreahaubner.blog.com/2011/03/27/trygetvalue/

The Mobile Zone is brought to you in partnership with Strongloop and IBM.  Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud.


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}