0

在主窗口 XAML 中,我添加了资源 Image my_image。在代码中,找到它的函数 FindResource ,该函数返回一个非空值。但是里面的 img.Source 是空的。我究竟做错了什么?

//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;

更新:通过将程序集类型作为资源来解决问题。并且还必须创建 Image img_new的新实例。并给它一个从资源中获得的来源。然后正常情况下,我们可以使用img_new

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

1 回答 1

2

将 device1.png 的 Build Action 更改为 Resource from Content

于 2013-02-13T06:52:24.367 回答