如何序列化附加属性
我正在使用复合应用程序库编写.NET3.5、WPF 应用程序。应用程序分为几个模块。
在基础设施模块中,我定义了 NetworkNode 对象。 Network 模块管理 NetworkNode 的集合,并使用 XmlSerializer 来存储/加载该集合。到目前为止一切正常。
但我还有其他模块,例如 NodeModule。如果在网络模块中选择了网络节点,则使用 EventAggregator 将事件发布到其他模块。这些模块可以使用附加属性将各种信息附加到 NetworkNode。
问题是 NetworkModule 不知道其他模块,因此这些属性没有序列化。是否可以以某种方式列出并序列化附加到对象的所有属性?或者我是否必须改变概念并使用附加属性以外的其他东西?
问候
I am writing .NET3.5, WPF application using Composite Application Library. Application is divided into several modules.
In infrastructure module I have defined NetworkNode object. The Network module manages a collection of NetworkNodes and uses XmlSerializer to store/load this collection. So far everythings works.
But I have other modules e.g NodeModule. If a NetworkNode was selected in Network module, an event is published to other modules using EventAggregator. These modules can attach various information to the NetworkNode using attached properties.
The problem is the NetworkModule does not know about the other modules, therefor these properties are not serialized. It is possible to somehow list and serialize all properties attached to an object? Or do I have to change the concept and use something else than attached properties?
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
DependencyObject.GetLocalValueEnumerator
:但是,这对 XML 序列化没有帮助(除非您实现 IXmlSerialized,这很痛苦......)。您可能应该使用
XamlWriter
相反(我认为这就是 Drew 所说的,因为没有XamlSerializer
...)You can list all dependency properties (attached or not) defined on an object using
DependencyObject.GetLocalValueEnumerator
:However, this won't help for XML serialization (unless you implement IXmlSerializable, which is a pain...). You should probably use
XamlWriter
instead (which I assume is what Drew was talking about, since there is noXamlSerializer
...)由于附加属性从纯 CLR 角度来看是不可见的,因此 XmlSerializer 无法了解它们。您需要切换到使用 XamlSerializer 体系结构,以便能够序列化“普通”CLR 对象并拥有 DependencyObjects 的特殊知识。
Since attached properties aren't visible from a pure CLR perspective, the XmlSerializer has no way to know about them. You would need to switch to use the XamlSerializer architecture in order to be able to serialize both "plain" CLR objects as well as have the special knowledge of DependencyObjects.
如果您使用的是.Net 4.0(我相信它们不在.Net 3.5中)
您可以使用 IAttachedPropertyStore 或 AttachablePropertyServices
参考示例 #1:.Net 4.0 中的 XAML :附加属性、IAttachedPropertyStore 和 AttachablePropertyServices
另外,一般来说,附加属性必须正确定义:
参考示例#2:
If you are using .Net 4.0 (I believe they aren't in .Net 3.5)
You can use either IAttachedPropertyStore or AttachablePropertyServices
Reference Example #1: XAML in .Net 4.0: Attached Properties, IAttachedPropertyStore and AttachablePropertyServices
Also, generally, the attached property must be defined correctly:
Reference Example #2: