Windows Phone 7 (Mango) Tutorial - 8 - What is Page State?


Sometime you may want to save the values present in your page while switching from one page to another page, sometime you may want to store the values while switching application between foreground and background.

 

When you are switching from one application to the other, the first app enters into the deactivated mode. At that time you can store the current state of the page and display them back again by retrieving those values from page state once you return to the first app.

 

In this chapter we will discuss about the page state. This will help you to know the basics about it, very good for a beginner to jump start with it.

 

Index - Windows Phone 7 (Mango) Tutorial

 

What is Page State?

As described above, you can store the current state of the page while switching to a different page or application and retrieve it back once you return to the same page. You can do this by using the Page State. You can use the "State" property present in the PhoneApplicationPage class to implement the same behavior.

 

Let's have a look into the definition of the State property:

 

image

 

Here you can see that, State is a public property of type IDictionary<string, object> present in the PhoneApplicationPage class. As you already know that, PhoneApplicationPage is the root of any page in Windows Phone application, hence you will be able to find this in all the pages. If you want, you can read more about the Page and PhoneApplicationPage class here:

If you look into the property declaration, you will come to know that, it has only Getters, that means, you can get the state but can't reinitiate the property.

 

As this is a Dictionary object, you can use the normal operations that you do with dictionary like Add, Remove, Clear etc.

 

Working with Page State

If you want to set any value to the State, you can use the following code snippet:

 
public void SetPageState(string key, object value)
{
    State[key] = value;
}

 

Similarly, you can use the following code snippet to retrieve the state value:

 
public object GetPageState(string key)
{
    return State.ContainsKey(key) ? State[key] : null;
}

 

End Note

Hope, this gave you basic idea about Phone Application's Page State. Now you will be able to do the basic operation with the state and play with the code. Tomorrow we will explore it in depth with a sample application. After that, this chapter will be clear to you before you jump start with your actual application.

 

Till then, Happy Coding. If you have any queries, drop a line here. I will try to address them as early as possible. If you are already in Twitter, follow me @kunal2383.


6 comments

  1. You have to remove the key from state once consumed.

    Best,
    Alex

    ReplyDelete
  2. Yes, this is just to make sure that you are not using the same key/value every time. BTW, that was just the basic showcase of what we can do with Page State.

    Stay tuned for my tomorrow's post more on Page State.

    ReplyDelete
  3. Thank you for sharing such relevant topic with us. I really love all the great stuff you provide. Thanks again and keep it coming.

    ReplyDelete
  4. I have read most of your blog posts and I would like to tell you how useful they are not only for me but for all of your readers.

    ReplyDelete
  5. Thank you Kunal.

    I have two pages Page1.xaml and Page2.xaml. I navigated to a web page(google.com) and then I used NavigationService.NavigateTo() to go to Page2 from Page1.xaml page. But when i return to the browser page using NavigationService.GoBack() or by pressing the back button. I see the Page1.xaml but it is blank(webbrowser control is white screen). How do i save the state of the Webbrowser so as it display the old page when i return from another page within my application ? I don't want to reload page again because there may be a state where user has entered values in html text fields and he goes to home page and comes back, entered information is lost.

    Regards,
    Simmy

    ReplyDelete
  6. Hello Simmy,

    Find out more about PageState, ApplicationState and Tombstoning here: http://www.kunal-chowdhury.com/2011/07/31-days-with-wp7-collection-of-windows.html and verify which one suits your requirement.

    Regards,
    Kunal

    ReplyDelete


 
© 2008-2014 Kunal-Chowdhury.com | Designed by Kunal Chowdhury
Back to top