gpt4 book ai didi

c# - 保存设置时未保存 OrderedDictionary

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

在我的应用程序中,我实现了一些控件 以不同方式浏览数据。在每个控件中,我都显示一个 TreeView 以允许用户从一个文件夹转到另一个文件夹。

我希望我的控件以通用方式“记住”最后选择的树(我的意思是,如果将来我添加另一个控件,我不想做很多调整)。所以我在设置中添加了一个 OrderedDictionary。我使用控件的类型名称作为键,使用节点的路径作为值。

因为我无法为这本字典设置默认值,所以我使用了这个技巧:

设置.cs:

public OrderedDictionary Paths
{
get
{
return LastsPaths ?? (LastsPaths = new OrderedDictionary());
}
set
{
this["LastsPaths"] = value;
}
}

Settings.Designer.cs:

[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public global::System.Collections.Specialized.OrderedDictionary LastsPaths {
get {
return ((global::System.Collections.Specialized.OrderedDictionary)(this["LastsPaths"]));
}
set {
this["LastsPaths"] = value;
}
}

每次添加/更新值时,我都会调用Saveuser.config 文件时间戳会更改,但内容保持不变:

<setting name="LastsPaths" serializeAs="Xml">
<value />
</setting>

它不适用于:

  • 应用处于 Debug模式;
  • 应用处于 Release模式;
  • 已安装应用。

我该如何解决这个问题?

最佳答案

似乎 OrderedDictionary(以及一般的通用词典)are not XML serializable .

您可以将其包装在另一个手动执行序列化的类中。这样,您就不会将字典直接暴露给 XML 序列化程序。你必须实现 IXmlSerializable实现这一目标。

关于c# - 保存设置时未保存 OrderedDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34176117/

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