2015-04-27 41 views
1

我有這樣的代碼:轉換的ImageSource串

if (MyImage.Source == "ms-appx:///Assets/myimage.png") 
{ 
    //do something 
} 

但我得到這個錯誤:

Cannot implicitly convert type 'string' to 'Windows.UI.Xaml.Media.ImageSource' 

所以,我怎麼能轉換ImageSource的字符串,所以我可以對它們進行比較?我試過ToString(),但它似乎不工作。

+0

圖像綁定到名爲Picture的類的字符串屬性。 我在XAML中有這個 圖像綁定到一個類的屬性 – user2975038

+0

對於初學者,您需要'=='而不是'='來進行比較。 – vesan

+0

你是對的我在寫這篇文章時沒有注意到。 – user2975038

回答

0

該圖片的Source屬性是ImageSource類型,而不是字符串。

由於每documentation for ImageSource

An object that represents the image source file for the drawn image. Typically you set this with a BitmapImage object, constructed with the URI that describes the path to a valid image source file. Or, you can initialize a BitmapSource with a stream, perhaps a stream from a storage file.

[更新時間:] 因此,你需要做兩件事情: 1.確定圖像源的類型 2.如果它是一個BitmapImage,檢查位圖的UriSource與預期的網址:

var imgUri = new Uri("ms:app:///Assets/SplashScreen.scale-100.png"); 
img.Source = new BitmapImage(imgUri); 

if (img.Source.GetType() == typeof(BitmapImage) 
    && (img.Source as BitmapImage).UriSource.Equals(imgUri)) 
{ 
    // Do something useful here! 

} 

HTH。

+0

我知道。我不想改變圖像。我只想看看舊的圖像路徑是否與「ms-appx:///Assets/myimage.png」相同。 – user2975038

相關問題