Wednesday, January 4, 2012

How To Using Isolated Storage In Windows Phone 7

0 comments

Isolated storage is a local storage that can be used for Windows Phone application data storage needs. When an application is running, it works with a number of data, some of which is temporary and only required for certain sessions, therefore storing them will not be necessary. This is where isolated storage comes in handy.
The whole IO operation has to use IsolatedStorage and for security reasons applications cannot access operating system storage and other application’s space. The figure below shows the storage structure in Windows Phone applications.
Using Isolated Storage In Windows Phone 7
There are two parts of storage, which are standard file folder and a particular folder to store application settings.
We should put into account that storage space in mobile devices is very limited. However Windows Phone does not give quota to applications in order to provide high flexibility for developers. Nevertheless, as a developer we should consider the space storage usage responsibly. Every temporary file should immediately be deleted, and users should be given the liberty to delete what they store.
It would be better to give transparency to users, letting them know how much space is used in the mobile device. We can also consider the option of storing in cloud storage, whether it’s in our own application server or in a third party server. Make sure applications on device are thin, making them comfortable to use. Would users use applications that take up a lot of space in their mobile device? That’s how smart phones lose their smartness :)

Isolated Storage For Files

If you continue from the previously made project, then we need a new page to learn about isolated storage, but if you don’t, create a new project for this matter. If you have learnt everything up to this page that should be relatively easy to do.
1 – Right click on Project, Add New Item and select Windows Phone Portrait Page. Rename the file as you like, in this example it’s called IsolatedStorage.xaml, then choose Add.
Isolated Storage For Files
2 – Insert a Button and a TextBox. Click event button will later be set so that it will store the string inside the TextBox into isolated storage.
Isolated Storage For Files
3 – Next, insert a TextBlock and a Button. By clicking the second button, we will call any string stored in isolated storage.
Isolated Storage For Files
4 – Now double click on the first button to handle storing data into isolated storage. Add these namespaces:
Isolated Storage For Files
And type in the following code:
Isolated Storage For Files
What the above code does is calling an application specific isolated storage then creates a folder called Data. The folder creation is meant for data organization simplicity. The code on the next line creates a stream writer with an isolated storage file as an input then stores the value from textBox1. Pretty straightforward.
5 – Next, double click on the second button to handle loading data from isolatedstorage. Type in the code below:
Isolated Storage For Files
6 – Press F5 and let’s see how the application works.
Isolated Storage For Files

Isolated Storage For Application Setting

Some applications have the need to store user inputs to be used later when the application is restarted. This scenario is useful for data such as user preferences, URL, or common information. Like other .NET applications, Window Phone also supports application setting storage. For this purpose we can useIsolatedStorageSettings.ApplicationSettings. Application Setting itself is an instance of IEnumerable.
1 – There is no need for a new page, let’s just continue from IsolatedStorage.xaml for this purpose. On the first button’s event handler, change the code into the following:
Isolated Storage For Application Setting
The code above is pretty straightforward: we call for an instance of ApplicationSetting, insert a value and a key then store it.
2 – On the second button’s event handler, change the code into the following:
Isolated Storage For Application Setting
The code above will call an instance of ApplicationSetting, try to fetch the value of duration, and display it with a MessageBox.
3 – Press F5 for results.
Isolated Storage For Application Setting
Press the first button to store the application’s setting. Nothing seems to happen, but do believe that the setting has been saved. To prove this, press the second button. A MessageBox will appear, showing the value stored in the application’s setting.
To delete the setting, it is also quite simple.
Isolated Storage For Application Setting

Leave a Reply