2011-04-21 60 views
13

我正在开发Windows Phone应用程序。为什么XAML图像源属性包含“/ MyApp; component”?

我使用的图像,当我选择使用属性面板中,我得到以下XAML图片:

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

为什么会出现“/MyApp;component/...”? (有没有更好的办法?)

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

回答

39

这是因为您的图像已将其构建操作设置为资源(这是默认设置)。如果你把它切换到内容您可以设置源在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; 

您应该使用性能内容原因。看到这篇文章的更多详细信息:http://www.windowsphonegeek.com/tips/wp7-working-with-images-content-vs-resource-build-action

0

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

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

您可以使用:

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

'FrameworkElement.BaseUri'仅适用于新的Windows Phone 8.1的Windows答存储应用;不过,这个请求是关于Windows Phone 7应用程序的。 – 2014-06-04 13:52:04

相关问题