在我的应用程序中,我实现了一些Controls
以不同的方式浏览数据。在每个Control中,我显示TreeView
以允许用户从文件夹转到另一个文件夹。
我希望我的控件以通用的方式“记住”最后选择的树(我的意思是,如果将来我添加另一个控件,我不想做很多改编)。所以我在“设置”中添加了OrderedDictionary
。我使用Control的Type Name作为键,Node的路径作为值。
由于我无法为此词典设置默认值,所以我使用了这个技巧:
Settings.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;
}
}
每次添加/更新值时都会调用Save
,user.config
文件时间戳会发生变化,但内容保持不变:
<setting name="LastsPaths" serializeAs="Xml">
<value />
</setting>
不适用于:
我该如何解决这个问题?
答案 0 :(得分:3)
似乎OrderedDictionary
(和一般的通用词典)are not XML serializable。
您可以将其包装在另一个手动执行序列化的类中。这样,您就不会将字典直接暴露给XML序列化程序。您必须实现IXmlSerializable
才能实现此目标。
答案 1 :(得分:0)
这可能足以让你入门。这会继承保存调用期间所需的IXMLSerializable
。
[XmlRoot("PreviouslyVisitedPaths")]
public class PreviouslySelectedPaths : OrderedDictionary, IXmlSerializable
{
#region Implementation of IXmlSerializable
/// <summary>
/// This method is reserved and should not be used. When implementing the IXmlSerializable interface, you should return null (Nothing in Visual Basic) from this method, and instead, if specifying a custom schema is required, apply the <see cref="T:System.Xml.Serialization.XmlSchemaProviderAttribute"/> to the class.
/// </summary>
/// <returns>
/// An <see cref="T:System.Xml.Schema.XmlSchema"/> that describes the XML representation of the object that is produced by the <see cref="M:System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter)"/> method and consumed by the <see cref="M:System.Xml.Serialization.IXmlSerializable.ReadXml(System.Xml.XmlReader)"/> method.
/// </returns>
public XmlSchema GetSchema()
{
return null;
}
/// <summary>
/// Generates an object from its XML representation.
/// </summary>
/// <param name="reader">The <see cref="T:System.Xml.XmlReader"/> stream from which the object is deserialized. </param>
public void ReadXml(XmlReader reader)
{
var keySerializer = new XmlSerializer(typeof(object));
var valueSerializer = new XmlSerializer(typeof(object));
var wasEmpty = reader.IsEmptyElement;
reader.Read();
if(wasEmpty)
{
return;
}
while(reader.NodeType != XmlNodeType.EndElement)
{
reader.ReadStartElement("item");
reader.ReadStartElement("key");
var key = keySerializer.Deserialize(reader);
reader.ReadEndElement();
reader.ReadStartElement("value");
var value = valueSerializer.Deserialize(reader);
reader.ReadEndElement();
Add(key, value);
reader.ReadEndElement();
reader.MoveToContent();
}
reader.ReadEndElement();
}
/// <summary>
/// Converts an object into its XML representation.
/// </summary>
/// <param name="writer">The <see cref="T:System.Xml.XmlWriter"/> stream to which the object is serialized. </param>
public void WriteXml(XmlWriter writer)
{
var keySerializer = new XmlSerializer(typeof(object));
var valueSerializer = new XmlSerializer(typeof(object));
foreach(var key in Keys)
{
writer.WriteStartElement("item");
writer.WriteStartElement("key");
keySerializer.Serialize(writer, key);
writer.WriteEndElement();
writer.WriteStartElement("value");
var value = this[key];
valueSerializer.Serialize(writer, value);
writer.WriteEndElement();
writer.WriteEndElement();
}
#endregion
}
}
然后您将代码更改为:
public PreviouslySelectedPaths Paths
{
get
{
return LastsPaths ?? (LastsPaths = new PreviouslySelectedPaths());
}
set
{
this["LastsPaths"] = value;
}
}
您还需要LastsPaths
输入PreviouslySelectedPaths
。
这只是为了让您入门,您可能需要调整IXMLSerializable
方法并填写GetSchema()
的逻辑。