我一直在尝试创建一个使用ContentWrapperAttribute
的小概念证明,但我不能让XamlXmlReader或XamlObjectWriter尊重它,它只是抛出一个异常,说它无法添加“字符串“到集合。
我已经定义了一个像这样的小对象模型:
[ContentProperty("MyItems")]
public class RootClass
{
public RootClass()
{
this.MyItems = new MyItemCollection();
}
public MyItemCollection MyItems { get; set; }
}
[ContentWrapper(typeof(MyItemText))]
public class MyItemCollection : List<MyItem>
{
}
[ContentProperty("Text")]
public class MyItemText : MyItem
{
public string Text { get; set; }
}
public class MyItem
{
}
我正在尝试使用此代码加载Xaml:
string xml = @"<?xml version=""1.0"" encoding=""utf-16""?>
<RootClass xmlns=""clr-namespace:XamlTest;assembly=XamlTest"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
<MyItem />
<x:String>test</x:String>
</RootClass>
";
StringReader reader = new StringReader(xml);
XamlReader xamlReader = new XamlXmlReader(reader, new XamlSchemaContext());
XamlObjectWriter xow = new XamlObjectWriter(xamlReader.SchemaContext);
while (xamlReader.Read())
xow.WriteNode(xamlReader);
xow.Close();
RootClass result = (RootClass)xow.Result;
我得到一个System.Xaml.XamlObjectWriterException
声明:“为'XamlTest.MyItemCollection'类型的集合添加值会引发异常。”并且内部System.ArgumentException
例外声称:
“值”test“不是”XamlTest.MyItem“类型,不能在此通用集合中使用。”。
是否应该由XamlXmlReader或XamlObjectWriter拾取ContentWrapperAttribute
,还是我误解了这个属性的作用?如果没有,有没有人真正让这个工作?