Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Isolated Storage Application Settings Not Persisting after Application exit

I'm having a big problem using WP7 isolated storage and applicationsettings. I have been using code from Adam Nathan's 101 Windows Phone 7 apps volume 1 as a basis.

I have a settings page where values can be altered and whilst the application is still running these remain active and it all works perfectly. However, as soon as the app exits on my developer phone these are lost and the app restarts with the default settings.

I have no idea why these values are not persisting. Any help would be much appreciated.

Here is the code i've got, its from adam nathan's new book. I sent him a message on twitter and he said its to do with a data type that isn't serializable. I looked into this but i'm only using double and bool values.

public class Setting<T>
{
    string name;
    T value;
    T defaultValue;
    bool hasValue;

    public Setting(string name, T defaultValue)
    {
        this.name = name;
        this.defaultValue = defaultValue;
    }

    public T Value
    {
        get
        {
            //checked for cached value
            if (!this.hasValue)
            {
                //try to get value from isolated storage
                if (IsolatedStorageSettings.ApplicationSettings.TryGetValue(this.name, out this.value))
                {
                    //not set yet
                    this.value = this.defaultValue;
                    IsolatedStorageSettings.ApplicationSettings[this.name] = this.value;
                }

                this.hasValue = true;
            }

            return this.value;
        }

        set
        {
            //save value to isolated storage
            IsolatedStorageSettings.ApplicationSettings[this.name] = value;
            this.value = value;
            this.hasValue = true;
        }
    }

    public T DefaultValue
    {
        get { return this.defaultValue; }
    }

    //clear cached value;
    public void ForceRefresh()
    {
        this.hasValue = false;
    }
}

Further development:

I receive this error on exiting the application:

A first chance exception of type 'System.IO.IsolatedStorage.IsolatedStorageException' occurred in mscorlib.dll


ERROR FOUND: I'm an idiot and left out one exclamation mark! from the trygetvalue part.

like image 417
James Mundy Avatar asked May 20 '11 00:05

James Mundy


1 Answers

Could you please post your storage code so we could see exactly what's going on? In absense of that code, here's the code I use to save setting to local storage:

IsolatedStorageSettings isoStoreSettings = IsolatedStorageSettings.ApplicationSettings;
if (isoStoreSettings.Contains(key))
{
    isoStoreSettings[key] = value;
}
else
{
    isoStoreSettings.Add(key, value);
}
isoStoreSettings.Save();

My guess is that you're missing that last line that commits the changes to isolated storage settings to the materialized isolated store instead of just leaving them in memory. If that's not the case, please edit your post with the code so that we can help.

like image 150
Jared Harding Avatar answered Sep 28 '22 03:09

Jared Harding