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

Van*_*nel 13 xaml image windows-phone-7

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

我正在使用图像,当我使用"属性"面板选择图片时,我得到以下XAML:

<Image x:Name="GameImage" Margin="8" Source="/MyApp;component/Assets/Icons/GameImage.png"/>
Run Code Online (Sandbox Code Playgroud)

我为什么要" /MyApp;component/..."?(有更好的方法吗?)

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

the*_*ent 39

这是因为您的图像将其构建操作设置为Resource(这是默认值).如果将其切换到Content,您可以在XAML中设置源代码,如下所示:

<Image x:Name="GameImage" Margin="8" Source="/Assets/Icons/GameImage.png"/>
Run Code Online (Sandbox Code Playgroud)

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

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

出于性能原因,您应该使用内容.有关更多详细信息,请参阅此文章:http://www.windowsphonegeek.com/tips/wp7-working-with-images-content-vs-resource-build-action