wpf 找不到资源

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

Cannot locate resource

c#wpf

提问by piggy

I don't know exactly if it is a bug but i am getting all the time runtime IOException error saying Cannot locate resource.

我不知道这是否是一个错误,但我一直收到运行时 IOException 错误,提示无法定位资源。

I am loading some images in my app (c#, WPF) in canvas background depends on database state. The problem is, that i cannot load LAST (alphabeting sorted) file. I have for example 15 images in folder. I can load 14 first without problems. But the last one all the time throw exception.

我在画布背景中的应用程序(c#、WPF)中加载一些图像取决于数据库状态。问题是,我无法加载 LAST(按字母排序)文件。例如,我在文件夹中有 15 张图像。我可以毫无问题地先加载 14。但是最后一个一直抛出异常。

I am 100% sure, that i HAVE the image in the folder (see !image-printscreen below). And how i wrote. 14 first i can load without any problem, only the last one throw exception.

我 100% 肯定,我在文件夹中有图像(请参阅下面的 !image-printscreen)。还有我怎么写的。14 第一个我可以正常加载,只有最后一个抛出异常。

Is it a bug in WPF-c# or am I doing something wrong?

这是 WPF-c# 中的错误还是我做错了什么?

code what is throwing exception:

代码抛出异常:

canvas_status.Background = new ImageBrush(new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), @"Images\" + statusName + ".png")));

canvas_name.Background = new ImageBrush(new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), @"Images\" + statusName + bulheadName + ".png")));

error:

错误:

IOException was unhalded
Cannot locate resource 'view/images/panel_uzavreno_d.png'.

image (for higher resolution click on image right mouse button and click on SHOW IMAGE or something like that):

图像(要获得更高的分辨率,请单击鼠标右键单击图像并单击“显示图像”或类似的东西):

printscreen

打印屏幕

回答by Roman

I've had a similar issue: IOException, cannot locate a png resource which indeed existed in the assembly.

我有一个类似的问题:IOException,找不到确实存在于程序集中的 png 资源。

I found the solution by explicitly specifying the assembly name, even though the caller was in the same assembly as the resource.

我通过显式指定程序集名称找到了解决方案,即使调用者与资源在同一个程序集中。

Here's how it looks with a Pack URI syntax:

以下是 Pack URI 语法的外观:

pack://application:,,,/MyAssemblyName;component/MyResourcesFolder/MyImage.png

(For more about Pack URIs see http://msdn.microsoft.com/en-us/library/aa970069.aspx)

(有关 Pack URI 的更多信息,请参见http://msdn.microsoft.com/en-us/library/aa970069.aspx

Edit:One more thing I had to do after specifying the assembly name was to Cleanthe project. The problem returns after build but was resolved after cleaning the intermediate products. This is definitely a bug in Visual Studio.

编辑:指定程序集名称后我必须做的另一件事是清理项目。构建后问题再次出现,但在清洁中间产品后已解决。这绝对是 Visual Studio 中的一个错误。

回答by juFo

You need to set the build action of the image to the type 'Resource'.

您需要将图像的构建操作设置为“资源”类型。

Right-mouse click on the file >> Properties >> set 'Build Action' to 'Resource'

鼠标右键单击文件>>属性>>将“构建操作”设置为“资源”