Isolated storage application settings are not saved after the application exits

I have a big problem using WP7 sandbox and applications. I used code from Adam Nathan 101 Windows Phone 7 apps volume 1 as the basis.

I have a settings page where the values ​​can be changed, and while the application is still working, they remain active and everything works fine. However, as soon as the application reaches my developer's phone, they are lost, and the application restarts with the default settings.

I do not know why these values ​​are not stored. Any help would be greatly appreciated.

Here is the code I have, it is from the new adam nathan book. I tweeted him and he said that this should be done with a data type that is not serializable. I studied this, but I only use the 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 get this error when exiting the application:

The first exception of type "System.IO.IsolatedStorage.IsolatedStorageException" exception occurred in mscorlib.dll


ERROR FOUND: I am an idiot and left one exclamation point! from the trygetvalue part.

+5
source share
1 answer

, , ? , :

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

, , , . , , .

+8

All Articles