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

Last two days, we discussed about Windows Phone 7 Page State. We learnt about saving and retrieving state of a page whenever require. Page State defines the current visual state of application page. Page state is accessible within that page. But think about a condition where you want to save values to the state and want to access them across the application. Then what to do? You have the option to use the Application State in that case.


Application State stores data values which can be accessible by multiple pages. In this tutorial chapter, we will learn about Application State and in the next chapter, we will see an actual implementation of the same with a small demo. Read to know more about Application State.


Index - Windows Phone 7 (Mango) Tutorial


What is Application State?

As described earlier, you can store and retrieve values to and from Application State in order to access them from different pages across the whole application. Like Page state, you can also use property called "State" of Dictionary type to set/retrieve application state.


In every Windows Phone 7 page, more precisely in PhoneApplicationPage we have a dictionary called "State" which we use for page state. We already discussed about them in the earlier chapters. If you didn't read them, you can find the article links here:

To use the application state we have another property called "State" of type Dictionary in the PhoneApplicationService class. We can use this to store and get values to access them in multiple pages. PhoneApplicationService is part of the namespace Microsoft.Phone.Shell and if you open the class definition, you will find the following code snippet:


Code Snippet: Microsoft.Phone.Shell.PhoneApplicationService


As it is a dictionary object, you can do all the operation like Add, Remove etc. to or from the collection.


Practical Scenario

Let's take a practical scenario to understand the Application State in a broader way. For example, suppose we have two pages called "Page 1" and "Page 2". We want to modify something in the Page 2 and reflect the same in Page 1. In this case, we will store the modified value to state from page 2 and when we navigated back to the page 1, we want to retrieve them from the state and display it accordingly.


In this scenario, we can't use the page specific state, but we have to use the application specific state in order to access them globally across the app.


Here is the diagram of our practical scenario:


Application State


We will see the implementation of the same in next chapter of the series with a small demo. But before going to discuss in depth, let's discuss on the implementation part a bit. Let's learn how to store and retrieve value to and from Application State. As it is a dictionary object, you can access them as you do with the dictionary collection.


To store data in Application State, you can use the following code:


Code Snippet for Saving value to Application State


Retrieving state value is also easy but before retrieving check whether the supplied key is present in the collection, else it will through Null Reference Exception. Following code will show you the way to retrieve data from the Application State:


Code Snippet to Retrieve value from Application State


End Note

Hope this chapter was useful to learn about the Application State. This series is mainly intended for the beginners but also helpful for others too. Trying to explain as much as I can. Let me know, if you have any feedback and/or suggestion.


Stay tuned for my next post where we will see a small demonstration of the Application State. That will clear the concept more. Follow my blog for to learn more on such topics. If you are available in Facebook, don't forget to like my blog's fan page. You can find it here. I am also available on Twitter. Feel free to contact 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.