Jim @ imason

Internet Architect by day, environmentalist by night: Jim Schwartz @ imason.

Isolated Storage in Silverlight 2 Beta 2

  • Comments 2

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.

Leave a Comment
  • Is ApplicationSettings.Save() necessary? I thought that was supposed to be a Beta 1 requirement that would go away in Beta 2. In my simple tests, I also find that values seem to persist without calling Save directly.

    One other note- ApplicationSettings seems to be scoped per browser (same goes for SiteSettings). Values saved this way won't be accessible across browsers on the same PC. We still much use the less convenient UserStore for cross browser storage.

    -Todd

  • Thanks Todd; I had originally written that code when we were still in Beta 1. The call to the Save method is redundant as it works fine without it (Will update the blog to reflect this)

    That is a downer that ApplicationSettings are scoped per browser, but thankfully I haven't had to worry about that yet; the Silverlight apps I've built are using a browser plugin controlled by a custom desktop app, so it will always be the same browser. Thanks for pointing that out though; it's definitely something to keep in mind when designing a Silverlight app.