2017-04-08 40 views
0

我選擇從照片庫中的照片和我得到以下如何將圖片加載到Xamarin形式的按鈕中PCL?

AlbumPath:

資產庫://asset/asset.JPG ID = 106E99A1-4F6A-45A2-B320-B0AD4A8E8473 &分機= JPG

路徑:

/用戶/ MYNAME /庫/開發商/ CoreSimulator /設備/ 21CB035B-A738-4F74-B121-2DB2A6B5372A /數據/容器/數據/應用/ 3081A323-98AF-4EF6-95B9 -29D4C2CD8425/Documents/temp/IMG_20170408_111143.jpg

如何將此圖像分配給按鈕?我嘗試了以下。

var file = await CrossMedia.Current.PickPhotoAsync(
      new  Plugin.Media.Abstractions.PickMediaOptions 
          { 

          }); 


Button btn = new Button(); 
btn.Image = (Xamarin.Forms.FileImageSource)ImageSource.FromFile(file.Path); 

按鈕上沒有圖像顯示。任何幫助幫助表示讚賞。

謝謝。

+0

任何原因該圖像不在iOS和Android項目中? –

+0

@Joshua Poling。是的,該圖像來自我的Mac的照片庫。它不在設備上。這是它不工作的原因嗎? – Dev

+0

以下答案是否解決了您的問題? –

回答

0

部署應用程序後,應用程序將無法訪問您的Mac。對於應用程序使用圖片,它將需要成爲iOS應用程序中的捆綁資源。以下是在iOS應用程序中使用圖像的簡單示例。你一定要考慮只是添加一個手勢監聽器到你的圖像,而不是使它成爲一個按鈕。如果您嘗試在按鈕上使用圖像,則必須進行一些樣式調整才能看起來乾淨整潔。

圖像設置在iOS的資源文件夾

認沽圖像

image resource folder

確保bundledresource從圖像屬性中選擇。 enter image description here

圖像按鈕

public class ImageButton : ContentPage 
    { 
     private Button _imageBtn = new Button { Image = "icon.png", Text = "Sample button" }; 

     public ImageButton() 
     { 
      Content = new StackLayout 
      { 
       Children = 
       { 
        _imageBtn 
       } 
      }; 
     } 
    } 

enter image description here

圖像的TapGesture

public class ImageButton : ContentPage 
{ 
    private Image _imageBtn = new Image { Source = "icon.png" }; 
    private TapGestureRecognizer _imageTap = new TapGestureRecognizer(); 

    public ImageButton() 
    { 
     _imageBtn.GestureRecognizers.Add(_imageTap); 
     Content = new StackLayout 
     { 
      Children = 
      { 
       _imageBtn 
      } 
     }; 
     _imageTap.Tapped += (s, e) => 
     { 
      // handle the tap 
     }; 
    } 
} 

enter image description here