WPF UserControl 在引用项目中找不到 XAML 资源

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

WPF UserControl cannot find XAML resource in referencing project

wpfxamlresources

提问by Tor Haugen

In my WPF project i keep a user control in a separate library project. The user control accesses resources in a separate XAML file, like this:

在我的 WPF 项目中,我将用户控件保留在单独的库项目中。用户控件访问单独的 XAML 文件中的资源,如下所示:

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Resources/ViewResources.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <!-- Local styles here -->
    </ResourceDictionary>
</UserControl.Resources>

The resource file, ViewResources.xaml, resides in a folder in the control library project named Resources. It has the default build action (Page) and custom tool (MSBuild:Compile).

资源文件 ViewResources.xaml 位于名为 Resources 的控件库项目中的文件夹中。它具有默认构建操作 (Page) 和自定义工具 (MSBuild:Compile)。

The problem is when I reference the control library in my WPF application and use the user control. At runtime, I get the following XamlParseException:

问题是当我在 WPF 应用程序中引用控件库并使用用户控件时。在运行时,我得到以下 XamlParseException:

Set property 'System.Windows.ResourceDictionary.Source' threw an exception.

...which wraps the IOException:

...包装了 IOException:

Cannot locate resource 'resources/viewresources.xaml'.

How can I fix this? I have tried to change the resource file's build action to "content" and have it copied to the output directory (that works for files and similar "dumb" resources). But to no avail. Also, it doesn't work property in the user control then.

我怎样才能解决这个问题?我试图将资源文件的构建操作更改为“内容”并将其复制到输出目录(适用于文件和类似的“哑”资源)。但无济于事。此外,它在用户控件中不起作用。

Is there a better way to specify the path?

有没有更好的方法来指定路径?

Will I have to move the resource file to the application project (I'd rather not, as it belongs in the user control's domain).

我是否必须将资源文件移动到应用程序项目(我宁愿不这样做,因为它属于用户控件的域)。

回答by Tor Haugen

Found it.

找到了。

Turns out there is a better way to specify the path, Pack URIs. I changed the XAML to the following:

事实证明,有一种更好的方法来指定路径Pack URIs。我将 XAML 更改为以下内容:

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/RoutingManager;component/Resources/ViewResources.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <!-- Local styles here -->
    </ResourceDictionary>
</UserControl.Resources>

and that fixed it.

并修复了它。

回答by AdrianD

I thought it was worth posting this just in case anyone else is struggling with the same problem, as I've spent over two hours fighting with syntax, etc. only to find that the solution was dead simple, but not that apparent: When referencing a packed resource from another control library, it seems to work fine at design time, and even compiles without error, but fails at runtime with the 'Set property 'System.Windows.ResourceDictionary.Source' threw an exception' error. It turns out that simply referencing the resource assembly from your control library is not enough, you ALSO need to add a REFERENCE to the assembly containing the resource dictionary in you main application assembly, else it seems it does not get compiled into the application. (i.e. Startup Application (the one with app.xaml) -> Add Reference -> select assembly with referenced resource file/s).

我认为值得发布这个以防万一其他人遇到同样的问题,因为我花了两个多小时与语法等进行斗争才发现解决方案非常简单,但并不那么明显:引用时来自另一个控件库的打包资源,它似乎在设计时工作正常,甚至编译没有错误,但在运行时失败,并出现“设置属性 'System.Windows.ResourceDictionary.Source' 引发异常”错误。事实证明,仅仅从您的控制库中引用资源程序集是不够的,您还需要向包含主应用程序程序集中资源字典的程序集添加一个 REFERENCE,否则它似乎不会被编译到应用程序中。(即启动应用程序(带有 app.xaml 的应用程序)-> 添加引用 -> 选择带有引用资源文件的程序集)。

Hope this helps!

希望这可以帮助!

回答by AlexDev

In my case I had the ResourceDictionary and the UserControl on the same Library, but separate from the main application. What worked for me was specifying the name of the assembly in the format Adam suggested in the comment AND I had to change the ResourceDictionary in the project from Embedded Resourceto Page. I didn't try using the pack:// format, but I assume it would work too.

就我而言,我在同一个库中拥有 ResourceDictionary 和 UserControl,但与主应用程序分开。对我有用的是以 Adam 在评论中建议的格式指定程序集的名称,并且我不得不将项目中的 ResourceDictionary 从 更改Embedded ResourcePage. 我没有尝试使用 pack:// 格式,但我认为它也可以。

<ResourceDictionary Source="/AssemblyName;component/Assets/MyResource.xaml"/>

回答by Ian

I had the same error (IOException - file not found), which cost me a day of my life that I'll never get back.

我有同样的错误(IOException - file not found),这让我失去了一天的生命,我永远不会回来。

Using neither the simpler "/assemblyname..." nor the "pack://...." syntax worked for me.

使用更简单的“ /assemblyname...”和“ pack://....”语法都不适合我。

I was referencing the resource assembly in my main assembly correctly.

我在主程序集中正确引用了资源程序集。

The error disappeared when I changed my xaml resource file Build Action property to "Resource", as mentioned above.

如上所述,当我将我的 xaml 资源文件 Build Action 属性更改为“Resource”时,错误消失了。

However, I then encountered a XamlParseException at this line:

但是,我在这一行遇到了 XamlParseException:

<ImageBrush x:Key="WindowBackground" ImageSource="Images/gradient.png" />

(which I had hand-typed).

(这是我手写的)。

This left the xaml resource file I was trying to include with effectively an invalid dependency.

这使我试图包含的 xaml 资源文件有效地包含了无效的依赖项。

Oddlythe fix was to delete the ImageSource property I had typed, re-insert it BUT select the image from the pulldown menus that appear as a result.

奇怪的是,修复方法是删除我输入的 ImageSource 属性,重新插入它,但从结果出现的下拉菜单中选择图像。

Even though the resulting line appears exactly the same, it clearly isn't.

尽管结果行看起来完全相同,但显然不是。

Starting to dislike WPF (VS2013), but hope this helps.

开始不喜欢 WPF (VS2013),但希望这会有所帮助。

:0/

:0/

回答by LoBo

I had the same situation, but the Pack URIs didn't help me, I was still getting "Cannot locate resource..." exception in the referencing (executable) project. What helped me, was the setting of my ResourceDictionary files in the custom control library project as Embedded Resource.

我遇到了同样的情况,但是 Pack URI 对我没有帮助,我仍然在引用(可执行)项目中遇到“无法定位资源...”异常。对我有帮助的是将自定义控件库项目中的 ResourceDictionary 文件设置为Embedded Resource