问题
I've been looking at the XAML Images sample project and the XAML to load an image in that sample looks like this:
<Image Source="Assets/image1.jpg"/>
However, in my own project I find that I cannot load any images like that. If I try I get E_NETWORK_ERROR when I handle the ImageFailed. Instead, I discovered that I have to use the ms-appx:/// prefix like this:
<Image Source="ms-appx:///Assets/image1.jpg"/>
Then it works. Any ideas what's different about my project vs the sample that would cause this?
回答1:
Have you tried "<Image Source="/Assets/image1.jpg>"?
回答2:
M working on Windows phone 8 at i used this.
OrderImage1.Source = new BitmapImage(new Uri("///Assets/images/order_bread.png"));
回答3:
For declarative images you do not need the ms-appx namespace (images declared in XAML). For dynamic databound images, a new bitmap instance is created, and it requires this namespace.
A great way to tackle this is to derive your data objects from a base object that implements the namespace as a shared property:
Private Shared _baseUri As New Uri("ms-appx:'''")
'//image handling
Private _image As ImageSource
Private _imagePath As String
Public Property Image As ImageSource
Get
If Me._image Is Nothing AndAlso Me._imagePath IsNot Nothing Then
Me._image = New BitmapImage(New Uri(dataModelBase._baseUri, Me._imagePath))
End If
Return Me._image
End Get
Set(value As ImageSource)
Me._imagePath = Nothing
Me.SetProperty(Me._image, value)
End Set
End Property
回答4:
This behavior works - tested in Windows 8 RTM / Visual Studio 2012 RTM, and can confirm that the ms-appx:// prefix is not required. What builds are you using?
There might be an issue with how you are setting the image or what Build Action you are selecting for it.
Also, this is documented at How to load file resources (XAML) (Windows)
来源:https://stackoverflow.com/questions/12764376/windows-8-app-why-must-i-specify-ms-appx-in-my-image-source-url