2013-02-13 94 views
0

在主窗口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; 
+0

资源给我你没有做错什么。如果将图像放入网格中,您是否看到图像?当在网格中添加img时: – Klaus78 2013-02-13 06:42:30

+0

MyGrid.Children.Add(img); 获取一个例外:System.Windows.Markup.XamlParseException – Mixer 2013-02-13 07:06:05

+0

你在XAML中看到的与 ? – Klaus78 2013-02-13 07:17:40

回答

2

更改您的device1.png的生成操作,从内容

+0

谢谢。 现在img.Source不为空。 我在调试器中看到图像大小等。 但是,当尝试在网格中添加img时:MyGrid.Children.Add(img); 获取一个异常:System.Windows.Markup.XamlParseException – Mixer 2013-02-13 07:05:13

+0

我无法重现您的问题。它对我来说运行良好。你能分享更多的代码吗? – Manish 2013-02-13 07:15:40