问题描述
我正在尝试在松散 XAML 中使用 ResourceDictionary 并将其加载到运行时以向 WPF 应用程序提供模板和样式.我在本地目录中有可用的 XAML,并且正在使用 URI 在应用启动时向 App.Current.Resources.MergedDictionaries 添加一个新的 ResourceDictionary.
I am trying to use a ResourceDictionary in loose XAML and load it a runtime to provide templates and styles to a WPF app. I have the XAML available in a local directory and am adding a new ResourceDictionary to App.Current.Resources.MergedDictionaries in app startup using a URI.
当 XAML 开始解析时,它会在模板上爆炸,其中 TargetType 是来自正在使用它的程序集的自定义控件.
When the XAML goes to parse, it blows up on a template where the TargetType is a custom control from the assembly that is consuming it.
具体信息是:
无法从文本控件:自定义类型"创建类型".'
我已经在顶部的 ResourceDictionary 中映射了命名空间:
I already have the namespace mapped in the ResourceDictionary at the top:
xmlns:controls="clr-namespace:TEST.UI.WPF.Common.Controls"
有文章指出从松散 XAML 加载 ResourceDictionaries 是可能的,但我发现没有一篇文章能够解决这些松散 XAML 文件中的自定义类型.
There are articles out there stating that loading ResourceDictionaries from loose XAML is possible but none of them that I have found address custom types within those loose XAML files.
感谢任何帮助!
推荐答案
由于您是在运行时加载它并且它不是在项目中构建的,因此 ResourceDictionary 不知道您的程序集.您必须将程序集包含在命名空间中才能被识别.
Since you are loading this at runtime and it is not built within the project then the ResourceDictionary does not know of your assembly. You will have to include the assembly in the namespace for it to be recognised.
xmlns:controls="clr-namespace:TEST.UI.WPF.Common.Controls;assembly=Common"
在命名空间声明中添加;assembly=yourAssembly"后,您应该能够运行您的应用程序.
You should be able to run you app after you add ';assembly=yourAssembly' to the namespace declaration.
这篇关于WPF 松散 XAML ResourceDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!