XamlReader - 将多个 CLR 命名空间映射到单个 XML 命名空间
我有一个带有 AssemblyInfo.cs
的 WPF 项目,它将多个 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 命名空间下,则所有内容有效,但不幸的是这不是选择。有人为了动态加载 XAML 内容而将多个 CLR 命名空间映射到单个 XML 命名空间吗?
提前致谢!
I have a WPF project with an AssemblyInfo.cs
that groups multiple CLR namespaces into a single XML namespace:
[assembly: XmlnsDefinition("http://foo.bar", "MyLibary.Controls")]
[assembly: XmlnsDefinition("http://foo.bar", "MyLibary.Converters")]
In XAML this is used like so:
<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>
This works great when the XAML is instantiated normally, but now I am now trying to dynamically load XAML files from my project using XamlReader
.
The problem: I can't seem to map multiple CLR namespaces to a single XML namespace. It seems that the last definition added to the XamlTypeMapper
is the only one that persists (e.g. it clobbers previous registrations):
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
The error message is:
'Cannot create unknown type '{http://foo.bar}MyConverter'.'
If all put all my code under a single common CLR namespace, everything works but unfortunately this is not option. Has anybody mapped multiple CLR namespaces to a single XML namespace for the purpose of loading XAML content dynamically?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如上面的评论中提到的,解决方案是在调用 XamlReader.Load 之前手动加载程序集并一起删除类型映射器和上下文:
我会假设自从
XamlTypeMapper
> 使用程序集列表进行初始化,该类将负责加载程序集(也许确实如此),但AddMappingProccessingInstruction
的行为阻止了它的工作。As mentioned in the comments above, the solution is to manually load the assembly before invoking
XamlReader.Load
and removing the type mapper and context all together:I would have assumed since the
XamlTypeMapper
is initialized with a list of assemblies, that this class would be responsible for loading the assembly (and maybe it is) but the behaviour ofAddMappingProccessingInstruction
prevents this from working.