wpf pack://application:,,,/ResourceFile.xaml 从不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22289367/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:09:46  来源:igfitidea点击:

pack://application:,,,/ResourceFile.xaml Never Works

.netwpfresourcedictionary

提问by Heinrich

I have never been able to get this format of reference a Resource Dictionary to work. What am I missing.

我从来没有能够让这种引用资源字典的格式工作。我错过了什么。

Scenario:

设想:

Creating and assembly with some usercontrols in it.
At root have said file {root}/Themes/ColorThemes.xaml
The ColorThemes.xaml file has its build action set to Resource.
Then in in xaml file further down let say {root}/Controls/ButtonStyles/Themes/ButtonThemes.xaml(note the path is just an example) in this file I have the following bit of code:

在其中创建和组装一些用户控件。
根已经说过文件{root}/Themes/ColorThemes.xaml
ColorThemes.xaml 文件的构建操作设置为Resource
然后在 xaml 文件中进一步说{root}/Controls/ButtonStyles/Themes/ButtonThemes.xaml(注意路径只是一个例子)在这个文件中我有以下代码:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/Themes/ColorThemes.xaml"/>
</ResourceDictionary.MergedDictionaries>

As far as the documentationstates this should work (as I understand it), and while I am in the designer it seems to be fine (all the colors load etc) however when I then go and compile my application and run it I get this error {"Cannot locate resource 'themes/colorthemes.xaml'."} which is strange because it seemed to be using just fine in the builder. So I am quiet confused because it seems to not coincide with the documentation.
So if someone can explain to me what I am missing :)

文档而言,这应该有效(据我所知),虽然我在设计器中似乎很好(所有颜色都加载等)但是当我编译我的应用程序并运行它时,我得到了这个错误 {"无法找到资源 'themes/colorthemes.xaml'。"} 这很奇怪,因为它似乎在构建器中使用得很好。所以我很困惑,因为它似乎与文档不一致。
因此,如果有人可以向我解释我缺少什么:)

Note: if I change the previous code to

注意:如果我将之前的代码更改为

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="../../../Themes/ColorThemes.xaml"/>
</ResourceDictionary.MergedDictionaries>

It works fine, however for cleanese pack://application: .

它工作正常,但是对于 cleanese pack://application: 。

回答by safetyOtter

try:

尝试:

pack://application:,,,/YOURNAMESPACEHERE/ColorThemes.xaml

E.g.

例如

pack://application:,,,/Themes/ColorThemes.xaml

回答by Athari

Can't reproduce your problem. All these variations work fine:

无法重现您的问题。所有这些变化都可以正常工作:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/Themes/ColorThemes.xaml"/>
    <ResourceDictionary Source="/Themes/ColorThemes.xaml"/>
    <ResourceDictionary Source="../../Themes/ColorThemes.xaml"/>
</ResourceDictionary.MergedDictionaries>

Note that pack://application:,,,is optional and /refers to the root of the current assembly.

请注意,这pack://application:,,,是可选的,/指的是当前程序集的根。

Please provide complete minimal example which reproduces your problem.

请提供完整的最小示例来重现您的问题。