XamlReader - 将多个 CLR 命名空间映射到单个 XML 命名空间

发布于 2024-12-17 21:13:02 字数 1588 浏览 2 评论 0原文

我有一个带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

君勿笑 2024-12-24 21:13:03

正如上面的评论中提到的,解决方案是在调用 XamlReader.Load 之前手动加载程序集并一起删除类型映射器和上下文:

Assembly.Load("MyLibrary");
var rootNode = XamlReader.Load(memeoryStream) as FrameworkElement

我会假设自从 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:

Assembly.Load("MyLibrary");
var rootNode = XamlReader.Load(memeoryStream) as FrameworkElement

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 of AddMappingProccessingInstruction prevents this from working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文