C# 如何在Xaml中使用ContentWrapperAttribute

C# 如何在Xaml中使用ContentWrapperAttribute,c#,xaml,C#,Xaml,我一直在尝试创建一个使用ContentWrapperAttribute的小型概念验证,但我无法让XamlXmlReader或XamlObjectWriter尊重它,它只是抛出一个异常,表示无法向集合添加“字符串” 我定义了一个小对象模型,如下所示: [ContentProperty("MyItems")] public class RootClass { public RootClass() { this.MyItems = new MyItemCollectio

我一直在尝试创建一个使用
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
{
}
[ContentProperty(“MyItems”)]
公共类根类
{
公共根类()
{
this.MyItems=新的MyItemCollection();
}
公共MyItemCollection MyItems{get;set;}
}
[ContentWrapper(typeof(MyItemText))]
公共类MyItemCollection:列表
{
}
[内容属性(“文本”)]
公共类MyItemText:MyItem
{
公共字符串文本{get;set;}
}
公共类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;
stringxml=@”
测试
";
StringReader=新的StringReader(xml);
XamlReader XamlReader=新的XamlXmlReader(reader,新的XamlSchemaContext());
XamlObjectWriter xow=新的XamlObjectWriter(xamlReader.SchemaContext);
while(xamlReader.Read())
xow.WriteNode(xamlReader);
xow.Close();
RootClass结果=(RootClass)xow.result;
我得到一个
System.Xaml.XamlObjectWriterException
声明:“向'XamlTest.MyItemCollection'类型的集合添加值引发了异常。”和一个内部
System.ArgumentException
异常声明: 值“test”不是“XamlTest.MyItem”类型,不能在此泛型集合中使用。“”

ContentWrapperAttribute
应该由XamlXmlReader或XamlObjectWriter拾取,还是我误解了该属性的作用?如果没有的话,有人真的让它起作用了吗