简体   繁体   English

为什么在运行时找不到用于图像的StaticResource?

[英]Why isn't StaticResource for image found at runtime?

I've added an image to my Resources.resx file. 我已经在我的Resources.resx文件中添加了一个图像。 I reference it like this: 我这样引用它:

<Image Source="{Binding MyImage}"/>

In the designer, I do see a squiggly blue line under the above. 在设计器中,我确实在上面看到一条弯曲的蓝线。 It says 它说

The resource "MyImage" could not be resolved. 资源“ MyImage”无法解析。

At runtime, it throws this error: 在运行时,它将引发以下错误:

Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception. 在“ System.Windows.Markup.StaticResourceHolder”上提供值引发了异常。

Any ideas what I'm doing wrong? 有什么想法我做错了吗?

Don't use resx files with WPF, you'd have to add some code to make the interop work. 不要在WPF中使用resx文件,您必须添加一些代码以使互操作正常工作。 Images in resx files are primarily used for WinForms code. resx文件中的图像主要用于WinForms代码。

Set the Build Action to Resource in the file properties: 在文件属性中将“ 构建操作”设置为 资源 ”:

建立行动

Then just reference the file directly: <Image Source="../../Some/Relative/Path.png"/> 然后,直接引用文件即可: <Image Source="../../Some/Relative/Path.png"/>

Neither {Binding} nor {StaticResource} is needed there. 此处不需要{Binding}{StaticResource}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM