Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 设置图像。源不';从资源加载时不更新_Wpf_Image_Resources - Fatal编程技术网

Wpf 设置图像。源不';从资源加载时不更新

Wpf 设置图像。源不';从资源加载时不更新,wpf,image,resources,Wpf,Image,Resources,我在XAML中有这样的定义: <Image Name="AlbumArt" Source="/AlbumChooser2;component/Resources/help.png" /> 我有一个helper类,它可以在有图像的情况下查找图像(以位图图像的形式返回),或者在没有图像的情况下返回null: if (findImage.Image != null) { this.AlbumArt.Source = findImage.Image; // This works }

我在XAML中有这样的定义:

<Image Name="AlbumArt" Source="/AlbumChooser2;component/Resources/help.png" />
我有一个helper类,它可以在有图像的情况下查找图像(以
位图图像
的形式返回),或者在没有图像的情况下返回null:

if (findImage.Image != null)
{
    this.AlbumArt.Source = findImage.Image; // This works
}
else
{
    this.AlbumArt.Source = noImage; // This doesn't work
}
在找到图像的情况下,更新源并显示相册艺术。在没有找到图像的情况下,我不会显示任何内容-只是一个空白

我不认为是
AlbumArt.Source
的设置不对,而是加载
BitmapImage


如果我使用了不同的图像(例如,原始帮助图像),但即使我重新创建“无图像”图像,它也不起作用。可能有什么问题?

需要思考/尝试的一些想法:

no_image.png的Visual Studio设置是什么?它是“嵌入式资源”还是“内容”?您是否将其设置为“始终复制”或“更新时复制”

看起来像是将图像作为资源引用,但实际上是否已将其添加到项目的资源中?如果它是一个资源,我相信您应该将它的属性设置为“资源”和“从不复制”

如果将
UriKind
UriKind.Absolute
设置为磁盘上实际文件的路径,会发生什么情况

如果在调试器中将
noImage
设置为
Source
时查看它,它是否绝对非空,并且宽度/高度是否正确?

试试这个

当wpf编译到.exe资源目录成为.exe的一部分时,您无法从此代码访问该目录

BitmapImage noImage = new BitmapImage(
          new Uri("/AlbumChooser2;component/Resources/no_image.png",
                  UriKind.Relative))
因此,您必须使用此代码
Resources.no_image.png
并将其转换为BitmapImage


关于REV

图像已加载,并且具有正确的高度和宽度。构建操作为“无”-将其设置为“资源”修复了该问题。
BitmapImage noImage = new BitmapImage(
          new Uri("/AlbumChooser2;component/Resources/no_image.png",
                  UriKind.Relative))