Isolated Storage Application Settings Not Persisting after Application exit
Asked Answered
T

1

5

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.

Tryptophan answered 20/5, 2011 at 0:57 Comment(4)
Could you show us your Isolated Storage code?Supposition
done, have added it above, thanksTryptophan
Regarding the exception, check this thread: forums.create.msdn.com/forums/t/65662.aspx?PageIndex=1Supposition
thanks, I have already this article and though relevant does not really contain a solution to the problemTryptophan
Q
8

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.

Quoits answered 20/5, 2011 at 6:3 Comment(3)
The changes SHOULD be persisted automatically when the app is closed but I always explicitly call Save() myself too.Affect
Thanks a lot Jared, I was also making the same mistake.Greet
I also forgot the Save() method and then the compile threw an error indicating that I should use [DataContract] and [DataMember] attributes with my class to serialize.Burlington

© 2022 - 2024 — McMap. All rights reserved.