WPF 抛出“无法定位资源"加载图像时出现异常 [英] WPF throws "Cannot locate resource" exception when loading the image

查看:73
本文介绍了WPF 抛出“无法定位资源"加载图像时出现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WPF 窗口,在我的本地系统中有一个文件的背景图像.所以 XAML 文件看起来像这样:

I am having a WPF window, with a background image of one file in my local system. So the XAML file looks like this:

 <Window.Background>
    <ImageBrush ImageSource="/MYASSEMBLY;component/Resources/MyPic.png"/>
 </Window.Background>

在 XAML 设计器中,它实际上显示了此背景图像,因此该图像确实存在于 MYASSEMBLY\Resources 文件夹中.但是,在启动应用程序时 InitializeComponent() 函数调用中,它抛出异常

In the XAML designer it actually shows this background image, so the image does exist in MYASSEMBLY\Resources folder. However, in the InitializeComponent() function call when I launch the application, it throws the exception

找不到资源resources/mypic.png".

Cannot locate resource 'resources/mypic.png'.

所以我现在完全迷失了.设计和运行时的目录是否不同,因此在运行时中不存在?

So I am totally lost now. Are the directories different between the design and runtime so doesn't exist in the runtime?

推荐答案

在解决方案资源管理器中,将图像文件的 Build Action 属性设置为 Resource.然后清理解决方案并重建它.

In Solution Explorer set the image file's Build Action property to Resource. Then clean the solution and rebuild it.

这篇关于WPF 抛出“无法定位资源"加载图像时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆