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

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.



If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter, Facebook, Google+ and LinkedIn. I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.

31 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
  7. Page state are the states in which your app single page work starting to stop everything comes in it.SPSS Assignment experts

    ReplyDelete
  8. Thanks for sharing this useful tutorial steps.....
    Assignment Help

    ReplyDelete
  9. This comment has been removed by a blog administrator.

    ReplyDelete
  10. Thank you for sharing such a nice post which is a great tutorial, it is well displayed and unique in its own way. Highly appreciated.
    essay redoing help

    ReplyDelete
  11. Windows Phone 7 features a user interface based on a design system code named and commonly referred to as Metro.Essay writing service blog Essay writings are mainly used in bachelor’s course, while dissertation is normally applied to a doctorate or PhD etc.

    ReplyDelete
  12. I heard that there is a multiple large updates, the last being Windows Phone 7. i ahve seen some articles related to it in Term paper writing service which is quite informative.

    ReplyDelete
  13. Windows 7 is the currently running system. I was in search of affordable dissertation writing services for completing my dissertation works.I felt This post as quite informative. Thanks for sharing.

    ReplyDelete
  14. Great website pixelgun 3d. Keep posting more informative articles and posts like these. Keep sharing your good work.

    ReplyDelete
  15. Such a nice post, keep providing good resources.

    ReplyDelete
  16. Excellent read, Positive site, where did u come up with the information on this posting? I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work

    ReplyDelete

  17. I like the helpful info you provide in your articles. I will bookmark your weblog and check again here frequently.

    Verizon wireless promo code

    ReplyDelete
  18. I am very much pleased with the contents you have mentioned. I wanted to thank you for this great article.

    ReplyDelete
  19. thanks for sharing the article kunal

    ReplyDelete
  20. Nice! thank you so much! Thank you for sharing. Your blog posts are more interesting and informative. I think there are many people like and visit it regularly, including me.

    ReplyDelete
  21. Awesome work you have done here, I am very happy to read this nice post. You are a great writer and give us much information.

    ReplyDelete
  22. Well composed articles like yours renews my faith in today’s writers. You’ve written information I can ultimately agree with as well as make use of.

    ReplyDelete
  23. Interesting article! Thank you for sharing them! I hope you will continue to have similar posts to share with everyone!
    slither io

    ReplyDelete
  24. I enjoyed over read your blog post. Your blog have nice information, I got good ideas from this amazing blog. I am always searching like this type blog post. I hope I will see again

    ReplyDelete
  25. Windows Phone 7 features a user interface based on a design system code named and commonly referred to as Metro. know more

    ReplyDelete
  26. Excellent read, Positive site, where did u come up with the information on this posting? I have read a few of the articles on your website now, and I really like your style. Thanks a million and please also visit my blog -
    digital vs offset

    ReplyDelete
  27. The knowledge you share really changes me in life, I sincerely thank you for the things you have done, sure your blog will help more people. Sincerely thanks

    ReplyDelete
  28. Really structured and useful information. And everything is clear, thanks. I looked through some posts and must say, they are very interesting.

    ReplyDelete
  29. Thanks for your post! Through your pen I found the problem up interesting! I believe there are many other people who are interested in them just like me! How long does it take to complete this article? I have read through other blogs, but they are cumbersome and confusing. I hope you continue to have such quality articles to share with everyone!
    vex

    ReplyDelete

 
© 2008-2017 Kunal-Chowdhury.com - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top