Follow us on X (Twitter)  Like us on Facebook  Connect with us on LinkedIn  Subscribe to our YouTube Channel  Subscribe to our WhatsApp Group

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:




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.

Have a question? Or, a comment? Let's Discuss it below...


Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.