c# - IsolatedStorageSettings.ApplicationSettings 在先前存储两个 DateTimeOffset 对象后无法加载设置

标签 c# windows-phone-8

这是一个有趣的奇怪行为(阅读:错误)。我的简单测试应用程序中有以下两种方法:

    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 属性内)然后设置对象为空(我的值(value)观丢失了)。异常说:

Type 'System.DateTimeOffset' cannot be added to list of known types since another type 'System.Runtime.Serialization.DateTimeOffsetAdapter' with the same data contract name 'http://schemas.datacontract.org/2004/07/System:DateTimeOffset' is already present.

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

如果我将不同的类型保存到“栏”设置,一切正常。仅当我保存两个或多个 DateTimeOffset 值时才会出现此问题。作为一种解决方法,我可以保存所有手动序列化为字符串的 DateTimeOffset 值。不过,我想避免这种情况。

最佳答案

看来您确实发现了 AppliationSettings 对象的错误。如果您打算在 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 IsolatedStorageSettings.ApplicationSettings complex data

此外,您可以更简单地解决这个问题,并通过如下更改保存和加载方法来避免使用集合。

    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();
    }

关于c# - IsolatedStorageSettings.ApplicationSettings 在先前存储两个 DateTimeOffset 对象后无法加载设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15306853/

相关文章:

c# - nServiceBus QA 测试工具

c# - 如何在 C# .Net 中将空指针传递给 Win32 API?

windows-phone-8 - 将 Windows Phone 8 模拟器(在 VirtualBox 上)连接到 Visual Studio(在主机上)

c# - 在列表中查找 int 的索引

c# - 检查 IsDisposed 和 Disposing 时为 "Cannot access a disposed object"

c# - 有没有一种简单的方法可以手动解码 FlateDecode 过滤器以提取 PDF 中的文本? C#

c# - Windows.UI.Xaml 命名空间在 visual studio 2012 中不支持用于 windows phone 开发

c# - Windows Phone 8 和读/写文件

html - 在不影响移动 IE10 的情况下隐藏 Firefox 中响应式设计 View 的滚动条?

c# - MVVM 设计模式中的确认窗口