gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:19:27 25 4
gpt4 key购买 nike

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

    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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com