Windows-8.1

Isolated Storage in Windows phone app – Store data in IsolatedStorageSettings

This is the second post regarding Isolated Storage in Windows Phone app. In this post I am going to talk about How to store data in IsolatedStorgeSettings and perform different manipulation operations on it like reading ,writing and update.

Basically the easiest way to put data into Windows Phone IsolatedStorage is to use the IsolatedStorageSettings class which  is a Dictionary<TKey, TValue> that stores key-value pairs in isolated storage.  A typical use is to save settings, such as the number of times the user has opened the application, the no of items to be displayed in a listbox etc.

In the following post I will give you a example of storing the students name and marks in isolated storage with the help of single objects and composite objects.

The different steps that need to be followed are

1. First create a new project having the name Isolatedstoragesettings.

2. Then open the MainPage.xaml and remove the XAML code for the StackPannel named TitlePanel and replace it with the following code so as to change the page name and application name .


 

 

3. Now add the following Xaml code in the Grid named ContentPanel so as to create Four buttons to save and read the data from IsolatedStorageSettings in case of single and composite object.

 

 


4. Now open the page MainPage.xaml.cs.

5. Add the following namespace in the page given below.

6. Add the following code for the Save and Read single object data Button click event handler below the constructor of the page. The code describes how to save the value of name and marks in IsolatedStorageSettings and How to retrieve the data from IsolatedStorageSettings.

 



7. Now for composite object you need to create a sample class called Resultclass with the two properties name and marks.

8. Add the following code for the Save and Read composite object data Button click event handler below the constructor of the page. The code describes how to save the value of name and marks in IsolatedStorageSettings and How to retrieve the data from IsolatedStorageSettings.

 



9. Now run the application on emulator. You will see the following output.


10. Now click on the save button of single object data first to save the values to isolated storage and then on read button to read the values from IsolatedStorgeSettings. The Output is shown below.


Note: The next time you will click on save button it will throw an error because the value of key that is saved in isolated settings must be unique and you are violating it by saving the same key again. To avoid it use try catch or enclose the code in if clause given below.

 

11. Similarly click on click on the save button of composite object data first to save the values to isolated storage and then on read button to read the values from IsolatedStorgeSettings. The Output is shown below.

 

 

Note: If you are working on Universal app Project the following code will give you a syntax error. So as to correct it replace the following line of code.

 

Replace it with


12. I hope this post will be helpful to you. Download the full Project zip file from the link given below.

Download Full project file Isolatedstoragesettings.zip
Stay tuned with rest of posts given below.

Isolated Storage in Windows Phone app – Introduction
Isolated Storage in Windows phone app – Reading and saving text files
Isolated Storage in Windows phone app – Reading and saving images

Chetan Gulati

Currently am doing B TECH in IT from Delhi Technological University. App development is my passion. Believe in quality rather than quantity.

Read more:
How to create your first Windows Phone app
How to change the App name and Tile name of your Windows phone app
Data binding in Windows Phone app – One way binding
Close