While your Windows 8 Store application is running, you want to be saving data all the time. After all, the user can switch to another application at any time, and you only have five seconds to store out your data at that point.
While that is true for major data points, there are certain “status” values that you want to store only at the last minute. This might include which information is visible, what is selected, user input data, and so forth.
A great way to store that information is in an ApplicationDataContainer. This can be stored with your local settings or your roaming settings, but it requires no special permission from the operating system; these files are considered safe.
To see this, let’s create an application that stores, restores and deletes application data. Our UI is very simple, a text box to put information into, a textBlock to display recalled information, and three buttons:
The event handlers are very straight forward. When you click Save we get the settings from the TextBox and write it into the settings collection, and then we blank the text box:
Retrieving the text is equally simple, we pull the text out of the settings collection as an object, and if it is not null we turn it into a string,
Finally, deleting a setting requires just a call to the Remove method,That’s all it takes to manage your application state. Piece of cake. Easy as pie.
Jesse Liberty has three decades of experience writing and delivering software projects. He is the author of 2 dozen books and has been a Distinguished Software Engineer for AT&T and a VP for Information Services for Citibank and a Software Architect for PBS. You can read more on his personal blog or follow him on twitter
Subscribe to be the first to get our expert-written articles and tutorials for developers!