为什么XAML图像源属性包含“/MyApp;component”?

13

我正在开发一个Windows Phone应用程序。

我使用了一张图片,并且当我在属性面板中选择一张图片时,我会得到以下的XAML代码:

<Image x:Name="GameImage" Margin="8" Source="/MyApp;component/Assets/Icons/GameImage.png"/>

为什么我会得到 "/MyApp;component/..."?(是否有更好的方法?)

如果我尝试使用 Image.Source="Assets/Icons/GameImage.png",为什么它不起作用?

4个回答

39

这是因为您的图像的构建操作设置为 Resource(这是默认设置)。如果您将其切换为Content,则可以在XAML中如下设置源:

<Image x:Name="GameImage" Margin="8" Source="/Assets/Icons/GameImage.png"/>

要在代码中设置它,您可以这样做:

BitmapImage tn = new BitmapImage();
tn.SetSource(Application.GetResourceStream(new Uri(@"Assets/Icons/GameImage.png", UriKind.Relative)).Stream);
Image.Source = tn;

出于性能方面的考虑,你应该使用Content。 更多详情请参见此文章:http://www.windowsphonegeek.com/tips/wp7-working-with-images-content-vs-resource-build-action


0

你可以使用:

BitmapImage obj = new BitmapImage();
obj.UriSource = new Uri(mera_image.BaseUri,file.Path);

FrameworkElement.BaseUri 只适用于新的 Windows Phone 8.1 和 Windows Store 应用程序;然而,此请求是关于 Windows Phone 7 应用程序。 - Oleksandr Zolotarov

0

不要忘记添加:

using System.Windows.Media.Imaging;

BitmapImage tn = new BitmapImage();
tn.SetSource(Application.GetResourceStream(new Uri(@"Assets/Icons/GameImage.png", UriKind.Relative)).Stream);
Image.Source = tn;

0

任何标记为嵌入式资源的内容都是从程序集中加载的。因此,使用该资源的站点需要知道指定资源所嵌入的程序集。在您的情况下,这是 MyApp


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接