Isolated Storage on WP7 (I)

Here I will explain how to store data on WP7 though it will be cleared after a compele OS boot.
Actually, all you need is an ApplicationSetting in IsolatedStorage.
Here create a class which you can set the ApplicatonSetting thus storing your data

    public class ApplicationSettings
    {
        private static string m_myAppKey = “myAppKey”;
        public static ApplicationData myAppSettings
        {
            get
            {
                return GetKeyValue<ApplicationData>(m_myAppKey);
            }
            set
            {
                SetKeyValue(m_myAppKey, value);
            }
        }

        private static T GetKeyValue<T>(string key)
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains(key))
                return (T)IsolatedStorageSettings.ApplicationSettings[key];
            else
                return default(T);
        }

        private static void SetKeyValue<T>(string key, T value)
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains(key))
                IsolatedStorageSettings.ApplicationSettings[key] = value;
            else
                IsolatedStorageSettings.ApplicationSettings.Add(key, value);

            IsolatedStorageSettings.ApplicationSettings.Save();
        }
    }

Then in code behind, forexample, I want to save a List called myList

In the saving part, what you have to write is

ApplicationSettings.myAppSettings = myList;

In the loading part, which you have to load the previously stored myList, just call

List myPreviousList = new List();
myPreviousList = ApplicationSettings.myAppSetting;

Of course, you may check if the myAppSetting is null or not before loading the data.

It is just one of the ways to store the data in WP7, tomorrow I will introduce another one. 🙂

Advertisements

5 thoughts on “Isolated Storage on WP7 (I)

  1. Pingback: Isolated Storage on WP7 (II) « Steve's Weblog

  2. What about Exceptions?

    ///
    /// Get the current value of the setting, or if it is not found, set the
    /// setting to the default setting.
    ///
    ///
    ///
    ///
    ///
    public valueType GetValueOrDefault(string Key, valueType defaultValue)
    {
    try
    {
    if (m_isolatedStore.Contains(Key))
    {
    return (valueType)m_isolatedStore[Key];
    }
    else
    {
    return defaultValue;
    }
    }
    catch (ArgumentException)
    {
    return defaultValue;
    }
    catch (Exception e)
    {
    MessageBox.Show(“Exception while using IsolatedStorageSettings: ” + e.ToString());
    }

    return defaultValue;
    }

    Reply
  3. Hi,

    I’m having some problem with Isolated Storage under WP7 Emulator.

    I lost data on Isolated Storage when press Start button and than reopen my App.

    There is a configuration file or any way to know why my emulator has this behaviour?

    thanks!

    Reply
  4. Pingback: Almacenamiento en Windows Phone 7 – Isolated Storage : Tecnologia en General y .NET en particular

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s