wpf 无法在运行时找到资源字典文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11691503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to find resource dictionary file at runtime
提问by Metro
I am developing a user control and use it inside an ElementHost. I define the resource dictionary as follows:
我正在开发一个用户控件并在 ElementHost 中使用它。我定义资源字典如下:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Themes/Classic.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
In my VS explorer, I have this
在我的 VS 资源管理器中,我有这个
Project (the user control library)
|_ Themes
|__ Generic.xaml (Build Action = Page)
|__ Classic.xaml (Build Action = Page)
There's no compilation error and the VS designer seems to pick up the resources defined in Classic.xaml
没有编译错误,VS 设计器似乎拿起了 Classic.xaml 中定义的资源
However, it crashes at runtime with the following exception:
但是,它在运行时崩溃,但出现以下异常:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Windows.Markup.XamlParseException: 'Set property 'System.Windows.ResourceDictionary.Source' threw an exception.' Line number '16' and line position '18'. ---> System.IO.IOException: Cannot locate resource 'themes/classic.xaml'.
System.Reflection.TargetInvocationException:调用的目标已抛出异常。---> System.Reflection.TargetInvocationException:调用的目标已抛出异常。---> System.Reflection.TargetInvocationException:调用的目标已抛出异常。---> System.Windows.Markup.XamlParseException: '设置属性 'System.Windows.ResourceDictionary.Source' 引发异常。行号“16”和行位置“18”。---> System.IO.IOException: 找不到资源“themes/classic.xaml”。
What's going on?
这是怎么回事?
回答by Metro
I ended up having to use the syntax that includes the assembly name... don't ask me why
我最终不得不使用包含程序集名称的语法......不要问我为什么
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/assemblyname;component/Themes/Classic.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
回答by yo chauhan
Hi try it without slash , we use slash when Build Action is Content or other but for ResourceDictionary build Action is Page.
嗨,试试不带斜杠,当构建操作是内容或其他时,我们使用斜杠,但对于 ResourceDictionary 构建操作是页面。
<ResourceDictionary Source="Themes/Classic.xaml"/>
<ResourceDictionary Source="Themes/Generic.xaml"/>
I hope this will help. You must have some other names for these Resource Dictionaries because in Themes folder of the same project Generic.xaml is used as default and its resource can be accessed without mergeing it.
我希望这将有所帮助。您必须为这些资源词典使用一些其他名称,因为在同一项目的 Themes 文件夹中,默认使用 Generic.xaml 并且无需合并即可访问其资源。

