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

Why isn't StaticResource for image found at runtime?

我已将图像添加到我的 Resources.resx 文件中。我这样引用它:

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

在设计器中,我确实看到上面有一条波浪形的蓝线。它说

The resource "MyImage" could not be resolved.

在运行时,它抛出这个错误:

Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.

知道我做错了什么吗?

不要将 resx 文件与 WPF 一起使用,您必须添加一些代码才能使互操作工作。 resx 文件中的图像主要用于 WinForms 代码。

在文件属性中将 Build Action 设置为 Resource:

然后直接引用文件即可:<Image Source="../../Some/Relative/Path.png"/>

那里既不需要 {Binding} 也不需要 {StaticResource}