IsolatedStorageSettings.ApplicationSettings在先前存储了两个DateTimeO

本文关键字:两个 DateTimeO ApplicationSettings 在先前 存储 IsolatedStorageSettings | 更新日期: 2023-09-27 18:02:58

这里有一个有趣的奇怪行为(读作:bug)。我在我的简单测试应用程序中有以下两个方法:

    private void Save()
    {
        var settings = IsolatedStorageSettings.ApplicationSettings;
        settings["foo"] = new DateTimeOffset(2012, 12, 12, 12, 12, 12, TimeSpan.Zero);
        settings["bar"] = new DateTimeOffset(2011, 11, 11, 11, 11, 11, TimeSpan.Zero);
        settings.Save();
    }
    private void Load()
    {
        var settings = IsolatedStorageSettings.ApplicationSettings;
        string foo = settings["foo"].ToString();
        string bar = settings["bar"].ToString();
    }

当我运行我的应用程序,我可以调用Save,然后Load,我得到保存的值。然而,当我停止应用程序,再次启动它并尝试加载时,有一个第一次机会InvalidOperationException(在ApplicationSettings属性内),然后设置对象为空(我的值丢失了)。异常显示:

类型的系统。DateTimeOffset'不能添加到已知类型列表中,因为另一个类型'System.Runtime.Serialization '。DateTimeOffsetAdapter'与数据合约名称'http://schemas.datacontract.org/2004/07/System:DateTimeOffset'已经存在。

当我使用ISETool.exe查看保存到_ApplicationSettings文件的内容时,我可以看到有两个DateTimeOffset类型引用,这可能是问题所在。换句话说,IsolatedStorageSettings.Save()创建了一个损坏的文件,以后无法加载。

如果我将不同的类型保存为"bar"设置,一切都可以正常工作。只有当我保存两个或多个DateTimeOffset值时才会发生问题。作为一种变通方法,我可以保存所有手动序列化为字符串的DateTimeOffset值。我想避免这种情况

IsolatedStorageSettings.ApplicationSettings在先前存储了两个DateTimeO

看起来您确实发现了applicationsettings对象的一个bug。如果您打算在ApplicationSettings中存储DateTimeOffset值,那么这种方法将有效。

用你的设置创建一个类:

    public class MyAppSettings
    {
        public DateTimeOffset Foo { get; set; }
        public DateTimeOffset Bar { get; set; }
    }

修改方法如下:

    private void Save()
    {
        Collection<MyAppSettings> myAppSettings = new Collection<MyAppSettings>();
        myAppSettings.Add(new MyAppSettings
        {
            Foo = new DateTimeOffset(2012, 12, 12, 12, 12, 12, TimeSpan.Zero),
            Bar = new DateTimeOffset(2011, 11, 11, 11, 11, 11, TimeSpan.Zero)
        });
        IsolatedStorageSettings.ApplicationSettings["MyAppSettings"] = myAppSettings;
    }
    private void Load()
    {
        Collection<MyAppSettings> myAppSettings = (Collection<MyAppSettings>)IsolatedStorageSettings.ApplicationSettings["MyAppSettings"];
        string foo = myAppSettings.First().Foo.ToString();
        string bar = myAppSettings.First().Bar.ToString();
    }

但是,我想阅读这个答案,以获得在您自己的设置文件中存储这类信息的技术。windows phone 7 isolatedstoragessettings。ApplicationSettings复杂数据

此外,您可以更简单地实现这一点,并通过更改Save和Load方法来避免使用Collection,如下所示。

    private void Save()
    {
        MyAppSettings myAppSettingsSimple = new MyAppSettings
        {
            Foo = new DateTimeOffset(2012, 12, 12, 12, 12, 12, TimeSpan.Zero),
            Bar = new DateTimeOffset(2011, 11, 11, 11, 11, 11, TimeSpan.Zero)
        };
        IsolatedStorageSettings.ApplicationSettings["MyAppSettingsSimple"] = myAppSettingsSimple;
    }
    private void Load()
    {
        MyAppSettings myAppSettingsSimple = (MyAppSettings)IsolatedStorageSettings.ApplicationSettings["MyAppSettingsSimple"];
        txtFoo.Text = myAppSettingsSimple.Foo.ToString();
        txtBar.Text = myAppSettingsSimple.Bar.ToString();
    }