【发布时间】:2011-11-24 02:00:55
【问题描述】:
我有一个带有 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 命名空间下,一切正常,但不幸的是,这不是选项。是否有人将多个 CLR 命名空间映射到单个 XML 命名空间以动态加载 XAML 内容?
提前致谢!
【问题讨论】:
-
这个MSDN forum message 表明需要实际加载程序集才能使多个 CLR 命名空间工作。
-
谢谢,没有看到那个帖子。在 XamlReader.Load() 之前删除对 AddMappingProcessingInstruction 的所有调用并使用 Assembly.Load() 似乎已经成功了。
标签: wpf xaml xamlreader