Windows store apps 在Windows 8中,XamlReader无法加载带有自定义附加属性的xaml

Windows store apps 在Windows 8中,XamlReader无法加载带有自定义附加属性的xaml,windows-store-apps,winrt-xaml,attached-properties,xamlreader,Windows Store Apps,Winrt Xaml,Attached Properties,Xamlreader,我有自定义控件“CustomControl”,其中定义了自定义附加属性“CustomAttachedProperty”。控件正常工作,折叠xaml按其假定的方式呈现: <ns:CustomControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:ns="using:MyNamespace"> <TextBlock ns:CustomControl.CustomAttach

我有自定义控件“CustomControl”,其中定义了自定义附加属性“CustomAttachedProperty”。控件正常工作,折叠xaml按其假定的方式呈现:

<ns:CustomControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:ns="using:MyNamespace">
   <TextBlock ns:CustomControl.CustomAttachedProperty="0" Text="This element causes Parse Error" />
</ns:CustomControl>

当我尝试通过XamlReader.load()加载相同的xaml时,问题开始出现。问题的根源是附加属性。如果没有附加属性,XamlReader将无法正确加载字符串

在同一程序集中调用XamlReader.Load,其中定义了CustomControl


有人知道怎么回事吗?

我不能给出完整的答案,但问题在于新的xaml平台及其处理反射和元数据的方式。基本上,因为xaml平台是本机的,所以它无法访问反射来正确解析xaml

解决这个问题的方法是使用IXamlMetadataProvider,老实说,这是一个很难实现的问题。正常构建过程的一部分是为您的应用程序创建一个应用程序,但它不能与xaml reader和库一起正常工作。你最终需要自己实现一个。您可以在XamlTypeInfo.g.cs的obj/Debug文件夹中为您的应用程序选择一个

我的解决方案是创建一个小项目,其中包含我将在App.xaml.cs中解析的xaml,让编译器构建上述文件,然后将其删除以创建我自己的文件


如果你看这个,你可以看到我的结果。

非常感谢你,奈杰尔,我会检查建议的代码,看看它是否对我有用。