2014-10-30 102 views
3

It's easy to set a source for an image in Xamarin如何獲得Image.Source作爲字符串

using Xamarin.Forms; 
Image image = new Image; 
image.Source = "someImage.jpg"; 

但我不能做反向操作。
例如:給定一個圖像的源代碼已經設置,打印源代碼。

Console.WriteLine ("Print Image source ==> {0}",image.Source); 
Console.WriteLine ("Print Image source ==> {0}",image.Source.ToString()); 

... 和一些更不相關的組合。

任何人都可以告訴我如何從圖像中獲取源代碼(帶有字符串)。

+1

什麼是'Image',以及以何種方式讀取屬性不工作?我也看到你用'source'設置它,並用'Source'讀取它們 - 它們實際上是分開的屬性,還是這是一個錯字,在這種情況下,你能顯示你實際嘗試編譯的代碼嗎? – 2014-10-30 17:03:26

+0

第一個代碼段也不起作用。假設你在談論System.Drawing.Image,那麼沒有公共的'source'屬性。無論如何,BCL公共成員名稱永遠不會以小寫字母開頭。 – 2014-10-30 17:03:33

+0

@Asad:注意xamarin標籤。 http://iosapi.xamarin.com/?link=T%3aXamarin.Forms.Image – 2014-10-30 17:06:03

回答

8

Xamarin.FormsImage.Source屬性是類型的ImageSource的。

ImageSource的Xamarin.Forms有繼承這個類等幾類: -

  • FileImageSource
  • StreamImageSource
  • UriImageSource

您可以類型檢查Image.Source看到Image.Source正在使用的執行,然後投它,並訪問鑄造對象的屬性。

例如(假設ImageSource的FileImageSource),你會碰到這樣的: -

Xamarin.Forms.Image objImage; 
.. 
.. 

.. 
if (objImage.Source is Xamarin.Forms.FileImageSource) 
{ 
    Xamarin.Forms.FileImageSource objFileImageSource = (Xamarin.Forms.FileImageSource)objImage.Source; 
    // 
    // Access the file that was specified:- 
    string strFileName = objFileImageSource.File; 
} 
+0

非常感謝,這只是一種魔法。 :) – Loebre 2014-10-31 08:47:08

+0

爲我工作,非常感謝你 – 2016-11-29 09:22:35

1

它看起來像Xamarin.Forms.ImageSourceXamarin.Forms.ImageSource類型的財產,其中有一個隱含投從string。這就是爲什麼你可以做Image.Source = "someImage.jpg",但它沒有辦法返回,可能是因爲它只使用字符串來查找文件並加載它。如果你需要你加載的文件的名字,你必須自己跟蹤它。

相關問題