Introduction and best practices for IsolatedStorageSettings
Delete instructional text in italic
This article explains how to ... Replace the abstract text with a short paragraph (or sentence) describing what the topic covers.
Enter article metadata as described below. Note that this template can be placed anywhere in the article. Do not remove parameters that you do not use
In Windows Phone you have to store all your files and permanent values in special place - Isolated Storage (link). There is a special class for storing values: IsolatedStorageSettings. You can store there anything you want: user settings, layout state or value to determine if program us running first tume. In this tutorial we will create a simple application, that keeps a value in IsolatedStorageSettings and then extend it to look more professional.
- Run Visual Studio 2012
- Create new Windows Phone application (7.5 or 8.0)
- Navigate to MainPage.xaml and insert a checkBox into content grid: code here
- submit to checked and unchecked events
- now, when page is creating insert copying value from isolated storage settings to checkbox: If it is not exist - set true by default: code here
- When Checked - set setting to true and automatocally save it. Isolated Storage in windows phone is not thread safe, so you have to use locking the thread every time you doing something with isolated storage
- When unchecked - setting to false and save it again.
- Run the application: screenshot here
As you can see, everything works fine. But we have to check if value in storage exists, we have to call Save every time we update anything and we have to use lock and finnaly we have to keep the property key name. More simplier way to work with IsolatedStorageSettings is to create a wrapper class around it. It will save the settings every time we update any value (in a thread safe way), it will automatically setup default value if setting does not exist (and does not throw an exception when you will try to access not existent value), and ofcause it will keep the name of property, so we will provide it only once during property declaration. Creating IsolatedStorageProperty class - IsolatedStorageSettings wrapper: code here The class is pretty easy, but you can extend it in any maner you want. Using is simple as well: [code]static readonly IsolatedStorageProperty<bool> SwitchProperty = new IsolatedStorageProperty<bool>("switch",true);[code] code here As you can see, a lot of lines of code are gone - very cool, when you store a lot of values. Good practice is to create a special static class, that will contain all your settings in it, so you can access any property in a program in any time you want, like this: code here IsFirstRunProperty lets me determine, if application is running first time. If property does not exists - it is first run, and i initialize this property and do some work i want to do at first run, if value exists in isolated storage - application runs second or more time. code of using first run. screen shot of "Hello, this is first run DialogBox"
The "platform categories" will be displayed here in preview only - Copy paste relevant categories into text here
Add categories below using category selector.