FindResource返回空图像

本文关键字:图像 返回 FindResource | 更新日期: 2023-09-27 17:58:50

在主窗口XAML中,我添加了资源Imagemy_Image。在代码中,找到其函数FindResource函数返回一个非null。但在img内部。源为空。我做错了什么?

//xaml
    <Window.Resources>
        <Image x:Key="my_image" Source="Properties/images/device1.png"/>
    </Window.Resources>
//c# code
    Image img=this.FindResource("my_image") as Image;

更新:通过将程序集类型指向资源,解决了该问题。并且还必须创建一个新的Imageimg_new实例。并给它一个资源来源。然后恢复正常,我们可以使用img_new

 Image img=this.FindResource("my_image") as Image;
 Image img_new=new Image();
 img_new.Source=img.Source;

FindResource返回空图像

将设备1.png的构建操作从内容更改为资源