All about WP7 Isolated Storage - Store data in IsolatedStorageSettings
published on: 3/17/2011 | Views: N/A | Tags: IsoStore
by WindowsPhoneGeek
This is the third article from the “All about WP7 Isolated Storage ” series of short articles focused on real practical examples with source code rather than a plain theory. I am going to talk about how to store data in IsolatedStorageSettings.
- All about WP7 Isolated Storage – intro to Isolated Storage
- All about WP7 Isolated Storage - Folders and Files
- All about WP7 Isolated Storage - Store data in IsolatedStorageSettings
- All about WP7 Isolated Storage - Read and Save Text files
- All about WP7 Isolated Storage - Read and Save XML files using XmlSerializer
- All about WP7 Isolated Storage - Read and Save XML files using XmlWriter
- All about WP7 Isolated Storage - Read and Save Images
- All about WP7 Isolated Storage - Read and Save Captured Image
- All about WP7 Isolated Storage - Read and Save Binary files
- All about WP7 Isolated Storage - File manipulations
- All about WP7 Isolated Storage - Recommendations and Best Practices
- All about WP7 Isolated Storage - open source Databases and Helper libraries
To begin with lets first create a sample Windows Phone 7 application project. Next include the following namespaces in MainPage.xaml.cs (alternatively you can use the code in another page):
using System.IO.IsolatedStorage;
Basically the easiest way to put data into WP7 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 images to display per page, page layout options, and so on.
NOTE: IsolatedStorageSettings supports only key/value formed data storage!
Save string values in IsolatedStorageSettings
In this example we will store a string that represent a particular email:
public void SaveStringObject()
{
var settings = IsolatedStorageSettings.ApplicationSettings;
settings.Add("myemail", "support@windowsphonnegeek.com");
}Get string value from IsolatedStorageSettings
In this example we will retrieve the previously stored email string from the IsolatedStorageSettings:
//Retrieve email Data var location = settings["myemail"].ToString(); settings["myemail"] = "support@windowsphonnegeek.com";
Save composite objects in IsolatedStorageSettings
In this example we will store a composite object. We will create a sample class City which will represent data related to cities. After that we will save to the IsolatedStorageSettings information about a particular city.
public void SaveCompositeObject()
{
var settings = IsolatedStorageSettings.ApplicationSettings;
City city = new City { Name = "London", Flag = "uk.png" };
settings.Add("city", city);
}
public class City
{
public string Name
{
get;
set;
}
public string Flag
{
get;
set;
}
}
Get composite object from IsolatedStorageSettings
In this example we will retrieve the previously stored City object from the IsolatedStorageSettings.
//Retrieve City Data
City City1;
settings.TryGetValue<City>("city", out City1);
DataBinding to object stored in IsolatedstorageSettings
This example demonstrates how to implement databibding to a composite object stored in the IsolatedStorageSettings:
<TextBlock Text="{Binding Name}" FontSize="50"/>
<Image Source="{Binding Flag}" Stretch="None" HorizontalAlignment="Left"/>
//Retrieve City Data
City City1;
settings.TryGetValue<City>("city", out City1);
this.DataContext = City1;
Here is how the result should looks like:

Best Practices
Always check if the target object exists in the IsolatedStorageSettings before try to get it! Note that all Key/Value pairs in the IsolatedStorageSettings must be unique pairs, so before saving any value to the settings make sure that it has a unique key. You can using some simple checks like for example:
if(settings.Contains("myemail"))
{
...
}
You can find the full source code here: WP7IsolatedStorageSettingsExample
In this article I talked about storing data in the IsolatedStorageSettings. I hope that it was helpful. Stay tuned with the rest of the posts.
You can also follow us on Twitter @winphonegeek
Comments
Binding
posted by: whinston on 3/17/2011 2:29:44 PM
Thank you so mush about the binding example. I really appreciate your help.
hh
posted by: Artur on 4/9/2011 11:30:26 PM
I think very useful information is about binary files. In spite of the settings can cope with my task, I prefer to use .bin file with necessary struct as in C++.
Error: InvalidCastException
posted by: Tom on 1/25/2012 3:37:39 AM
I seem to be having a problem where when I try to get that values created on a different page, it crashes saying I have an InvalidCastException. This is on the line in the get method, settings.TryGetValue
I am wondering if there is a fix for this.
What is the path of Image i.e Flag = "uk.png"
posted by: sai on 2/8/2012 8:56:03 AM
In the above example can you please let me know what would be path of the Flag i.e uk.png
delete storage Data in isolatedStorageSetting
posted by: ram on 2/24/2012 9:16:22 AM
how to delete storage Data in isolatedStorageSetting.
posted by: sachin on 3/24/2012 12:12:32 PM
How to update key values ?
I see add option and remove option ? but I dont see any update method ?
database
posted by: g on 4/18/2012 11:18:09 AM
how to add database permanently in isolated storage
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
