3
我有一個AssemblyInfo.cs
一個WPF項目組的多個CLR命名空間到一個單一的XML命名空間:XamlReader - 映射多個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名稱空間?
在此先感謝!
這[MSDN論壇消息](http://social.msdn.microsoft.com/Forums/en-us/wpf/thread/6390a403-96b4-426d-a89a-92f20d577b75)表明該程序集需要實際上加載多個CLR命名空間才能工作。 –
謝謝,沒有看到那個線程。刪除所有對AddMappingProcessingInstruction的調用,並在XamlReader.Load()之前使用Assembly.Load()似乎已經完成了這個技巧。 –