In Silverlight 2 Beta 1, storing user settings in IsolatedStorage was almost as much of a pain as storing user settings in Vista Gadgets (Except you can write .NET code to stream the data to file instead of using JavaScript).
With the recent release of Silverlight 2 Beta 2, storing data in IsolatedStorage is as easy as storing and retrieving values from a web.config.
To store business objects into IsolatedStorage, I would recommend creating a Class in your Silverlight project to hold all of the user settings. You also need to set the “DataContract” attribute on any classes that you want to persist to IsolatedStorage as well as the “DataMember” attribute for the properties you’d like to persist. This will allow the class to be serialized into IsolatedStorage.
Here is an example of a simple entity class with some data that I’d like to persist:
using System.Runtime.Serialization;
namespace My.Silverlight.Application
{
[DataContract]
public class City
{
private string _cityName;
[DataMember]
public string CityName
{
get { return _cityName; }
set { _cityName = value; }
}
}
}
Next, I’ll store this City object in my “UserSettings” object along with the Silverlight user’s e-mail address.
using System;
using System.Collections.Generic;
namespace My.Silverlight.Application
{
public class UserSettings
{
private City _citySetting;
private string _userEmailAddress;
}
}
Lastly, I’ll clear the IsolatedStorage application settings, then add my UserSettings object and save to IsolatedStorage ApplicationSettings:
IsolatedStorageSettings.ApplicationSettings.Clear();
IsolatedStorageSettings.ApplicationSettings.Add("UserSettings", settings);
That’s it!
You could alternatively write string values into IsolatedStorage if you only need to store simple string values. The above examples shows you how to store business objects directly to IsolatedStorage. Keep posted for more articles on developing on Silverlight 2 Beta 2.