如果值为 NULL,如何说服 System.Xaml.XamlXmlWriter.WriteValue 触发
我使用 XamlServices.Transform 获取对象模型并将其序列化为 Xaml。
我已经实现了一个继承自 XamlXmlWriter 的类,它重写了 WriteValue。我使用它来将自定义 MarkupExtension 重新实例化回渲染的 Xaml 中。我的代码工作正常,除非属性的值为空,在这种情况下,WriteValue 不会触发,并且我没有机会“交换”重写类中的值。
一个相关的问题是属性与 System.ComponentModel.DefaultValue() 特性指定的值具有相同的值。例如,假设我的对象模型中有一个属性是这样装饰的:
[DefaultValue(true)]
public Boolean IsVisible {get; set;}
那么 WriteValue 方法仅在 IsVisible 属性为 false 时才会触发(这是有道理的)。
文档中的“备注”部分(http://msdn.microsoft.com/en-us/library/system.xaml.xamlxmlwriter.writevalue.aspx)提到了一些有关空值的内容,但我不明白:
输入值可能为空,即 支持显式写出 null 作为序列化值。这种行为 使用 XamlLanguage.Null 定义 作为 WriteStartObject 输入,然后 立即调用 WriteEndObject。
如何 a) 在属性为 null 时触发“WriteValue”,b) 在属性与 DefaultValue 属性相同时触发“WriteValue”?
我不确定它们是否相关,非常欢迎为它们中的任何一个提供解决方案。
谢谢,
丹尼尔
I'm using the XamlServices.Transform to take an object model and serialize it to Xaml.
I've implemented a class which inherits from XamlXmlWriter which overrides WriteValue. I'm using this to reinstantiate a custom MarkupExtension back into the rendered Xaml. My code works fine except when the value of the property is null, in which case the WriteValue doesn't fire and I don't get chance to "swap out" the value in the overriden class.
A related issue is where a property has the same value as that specified by the System.ComponentModel.DefaultValue() attribute. For example say i've got a property in my object model decorated like this:
[DefaultValue(true)]
public Boolean IsVisible {get; set;}
Then the WriteValue method only fires if the IsVisible property is false (which kind of makes sense).
The Remarks section in the documentation (http://msdn.microsoft.com/en-us/library/system.xaml.xamlxmlwriter.writevalue.aspx) mentions something about null values, but I don't understand it:
The input value may be null, which
supports explicitly writing out null
as a serialized value. This behavior
uses the XamlLanguage.Null definition
as WriteStartObject input and then
immediately calls WriteEndObject.
How to I a) make the "WriteValue" fire when the property is null, and b) make the "WriteValue" fire when the property is the same as the DefaultValue Attribute?
I'm not sure if they are related, a solution for either of them would be very welcome.
Thanks,
Daniel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先回答“b”:XamlObjectReader 的预期行为是跳过其值为声明的“默认值”的属性,并且我们没有配置功能来覆盖它。请注意,这里的“默认值”是使用 [DefaultValue()] 属性声明的值,而不是 C# 语言的 default(T),因此事情可能不会像您担心的那么糟糕。我的意思是,并非“Int”属性中的每个“0”都会被跳过,因为它是“默认值”。
'a':空值的 XamlXmlWriter 输出节点流不是“WriteValue(null)”,而是“WriteStartObject(nullExtensions); WriteEndObject()”。这是您引用的文档所描述的行为。所以你应该没问题。查找 StartObject“nullExtension”而不是值“null”。
To answer ‘b’ first: The XamlObjectReader’s intended behavior is to skip properties whose value is the declared “default value” and we have no configuration feature to override that. Note that “default value” here is the one declared with the [DefaultValue()] attribute, not the C# language default(T), so things may not be as bad as you fear. I mean not every “0” in an “Int” property is skipped because it is the “default value”.
‘a’: The XamlXmlWriter’s output Node stream for a null value is not “WriteValue(null)”, but instead is “WriteStartObject(nullExtensions); WriteEndObject()”. This is the behavior the documentation you quoted was describing. So you should be fine. Look for StartObject “nullExtension” instead of value “null”.