嵌入的图像在UserControl中不显示 [英] Embedded images not showing when in a UserControl

查看:256
本文介绍了嵌入的图像在UserControl中不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Image控件,其中包含嵌入图像的路径(构建操作'资源')。

I have an Image control that contains a path to an embedded image (build action 'resource').

<Image Source="Assets/images/image.png" Stretch="None" />

如果我将其添加到MainPage.xaml中的容器中,则图像显示正常。如下所示在UserControl中具有相同的图像,然后在MainPage.xaml上添加该UserControl的实例时,图像不会出现。

If I add that to a container in my MainPage.xaml the image appears fine. When having the same image in a UserControl as shown below, and then adding an instance of that UserControl on MainPage.xaml the image does not appear.

<UserControl x:Class="HomePage.Views.SimpleUserContol"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
    <Grid x:Name="LayoutRoot" >
        <Image Source="Assets/images/image.png" Stretch="None" />
    </Grid>
</UserControl>

任何人都可以解释为什么会发生这种情况并且可能会指出我的解决方案。

Can anyone shed any light on why this happening and maybe point me in the direction of a solution.

干杯,J

(我在Silverlight工作,但认为WPF中可能会发生同样的事情)

(I'm working in Silverlight but would think the same thing probably happens in WPF)

编辑:

设置

<Image Source="/Assets/images/image.png" Stretch="None" />

在将构建操作设置为内容时工作正常但是当它不起作用时使用'资源'。问题是它确实是文件结构中的相对位置,因为add ../工作正常。如果可能的话,我仍然想要一个从装配中获取图像的解决方案

推荐答案

你必须参考它作为资源,而不仅仅是路径。
这是在WPF应用程序中完成的方式:

You have to reference it as a resource, not just the path. This is how it is done in a WPF application:

<Image Source="/MyAppName;component/images/image.png" Stretch="None" />

原始图片位于 images / image.png

注意:

我没有SilverLight的经验,但你说它可能与WPF类似,所以我建议这样做。 ..

Note:
I have no experience in SilverLight, but you said it is probably similiar in WPF, so I suggest this...

这篇关于嵌入的图像在UserControl中不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆