2011-04-21 80 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

相關問題