如何将图片加载到Xamarin形式的按钮中PCL?

问题描述:

我选择从照片库中的照片和我得到以下如何将图片加载到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

以下答案是否解决了您的问题? –

部署应用程序后,应用程序将无法访问您的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