找不到资源 [英] Cannot locate resource
问题描述
我不知道这是否是一个错误,但我一直收到运行时 IOException 错误,提示无法定位资源.
I don't know exactly if it is a bug but i am getting all the time runtime IOException error saying Cannot locate resource.
我在画布背景中的应用程序(c#、WPF)中加载一些图像取决于数据库状态.问题是,我无法加载 LAST(按字母排序)文件.例如,我在文件夹中有 15 张图像.我可以毫无问题地先加载 14.但是最后一个一直抛出异常.
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.
我 100% 确定,我在文件夹中有图像(请参阅下面的 !image-printscreen).还有我怎么写的.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.
这是 WPF-c# 中的错误还是我做错了什么?
Is it a bug in WPF-c# or am I doing something wrong?
编写抛出异常的代码:
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")));
错误:
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):
推荐答案
我遇到了类似的问题:IOException,无法找到程序集中确实存在的 png 资源.
I've had a similar issue: IOException, cannot locate a png resource which indeed existed in the assembly.
我通过显式指定程序集名称找到了解决方案,即使调用者与资源在同一个程序集中.
I found the solution by explicitly specifying the assembly name, even though the caller was in the same assembly as the resource.
以下是 Pack URI 语法的外观:
Here's how it looks with a Pack URI syntax:
pack://application:,,,/MyAssemblyName;component/MyResourcesFolder/MyImage.png
(有关 Pack URI 的更多信息,请参阅 http://msdn.microsoft.com/en-us/library/aa970069.aspx)
(For more about Pack URIs see http://msdn.microsoft.com/en-us/library/aa970069.aspx)
在指定程序集名称后,我还必须做的另一件事是清理项目.构建后问题再次出现,但在清洁中间产品后已解决.这绝对是 Visual Studio 中的一个错误.
One more thing I had to do after specifying the assembly name was to Clean the project. The problem returns after build but was resolved after cleaning the intermediate products. This is definitely a bug in Visual Studio.
这篇关于找不到资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!