在主窗口XAML我添加资源图片my_image。 在代码中找到它的函数FindResource 该函数返回一个非null。 但是在img.Source中是空的。 我在做什么错?FindResource返回空图像
//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;
UPDATE: 这个问题是通过指向组件类型作为资源来解决。 并且还必须创建图像的新实例img_new。 并给它一个从资源中获得的东西的来源。 然后正常,我们可以使用img_new。
Image img=this.FindResource("my_image") as Image;
Image img_new=new Image();
img_new.Source=img.Source;
资源给我你没有做错什么。如果将图像放入网格中,您是否看到图像?当在网格中添加img时: – Klaus78 2013-02-13 06:42:30
MyGrid.Children.Add(img); 获取一个例外:System.Windows.Markup.XamlParseException – Mixer 2013-02-13 07:06:05
你在XAML中看到的与 ? –
Klaus78
2013-02-13 07:17:40