我有一个WPF项目,其中包含一个AssemblyInfo.cs
文件,把多个CLR命名空间分组成一个单独的XML命名空间:
[assembly: XmlnsDefinition("http://foo.bar", "MyLibary.Controls")]
[assembly: XmlnsDefinition("http://foo.bar", "MyLibary.Converters")]
在 XAML 中,它的使用方法如下:
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:fb="http://foo.bar">
<fb:FooButton IsEnabled="{Binding Something, Converter={fb:FooConverter}}"/>
</UserControl>
当XAML被正常实例化时,这很有效,但现在我正在尝试使用XamlReader
从我的项目中动态加载XAML文件。
问题在于:我似乎无法将多个CLR命名空间映射到单个XML命名空间。似乎最后添加到XamlTypeMapper
中的定义是唯一持久存在的(例如,它覆盖了先前的注册):
var parserContext = new ParserContext();
parserContext.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
parserContext.XmlnsDictionary.Add("fb", "http://foo.bar");
parserContext.XamlTypeMapper = new XamlTypeMapper(new string[] {"MyLibrary"});
parserContext.XamlTypeMapper.AddMappingProcessingInstruction("http://foo.bar", "MyLibrary.Converters", "MyLibrary");
parserContext.XamlTypeMapper.AddMappingProcessingInstruction("http://foo.bar", "MyLibrary.Controls", "MyLibrary");
...
var rootNode = XamlReader.Load(memeoryStream, parserContext) as FrameworkElement
错误信息是:
'Cannot create unknown type '{http://foo.bar}MyConverter'.'
如果我将所有代码放在一个公共的CLR命名空间下,一切都可以正常工作,但不幸的是这不是一个选项。有人将多个CLR命名空间映射到一个XML命名空间,以便动态加载XAML内容吗?
提前致谢!