应用程序退出后,独立存储应用程序设置不会保留

Jam*_*ndy 5 silverlight isolatedstorage windows-phone-7 c#-4.0

使用WP7隔离存储和应用程序设置时遇到了很大问题.我一直在使用Adam Nathan的101个Windows Phone 7应用程序第1卷中的代码作为基础.

我有一个设置页面,其中值可以更改,而应用程序仍在运行时,这些仍保持活动状态,这一切都完美无缺.但是,只要应用程序退出我的开发人员手机,它们就会丢失,应用程序会使用默认设置重新启动.

我不知道为什么这些值不会持续存在.任何帮助将非常感激.

这是我得到的代码,它来自亚当娜坦的新书.我在twitter上给他发了一条消息,他说这与一个不可序列化的数据类型有关.我调查了这个,但我只使用double和bool值.

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

进一步的发展:

退出应用程序时收到此错误:

mscorlib.dll中出现'System.IO.IsolatedStorage.IsolatedStorageException'类型的第一次机会异常


发现错误:我是个白痴,遗漏了一个惊叹号!来自trygetvalue部分.

Jar*_*ing 8

您能否发布您的存储代码,以便我们可以确切地看到发生了什么?在没有该代码的情况下,这是我用来将设置保存到本地存储的代码:

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

我的猜测是你错过了将隔离存储设置的更改提交到物化隔离存储的最后一行,而不是将它们留在内存中.如果情况并非如此,请使用代码编辑您的帖子,以便我们提供帮助.