如何将图像加载到xamarin forms PCL中的按钮上?

如何将图像加载到xamarin forms PCL中的按钮上?,xamarin,xamarin.forms,xamarin-studio,Xamarin,Xamarin.forms,Xamarin Studio,我从照片库中挑选了一张照片,我得到了以下信息 相册路径: 资产-library://asset/asset.JPG?id=106E99A1-4F6A-45A2-B320-B0AD4A8E8473&ext=JPG 路径: /用户/myname/Library/Developer/CoreSimulator/Devices/21CB035B-A738-4F74-B121-2DB2A6B5372A/data/Containers/data/Application/3081A323-98AF-4EF6-

我从照片库中挑选了一张照片,我得到了以下信息

相册路径:

资产-library://asset/asset.JPG?id=106E99A1-4F6A-45A2-B320-B0AD4A8E8473&ext=JPG

路径:

/用户/myname/Library/Developer/CoreSimulator/Devices/21CB035B-A738-4F74-B121-2DB2A6B5372A/data/Containers/data/Application/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);
按钮上不显示任何图像。感谢您的帮助


谢谢。

一旦部署应用程序,应用程序将无法访问您的mac。要让应用程序使用图片,它需要是iOS应用程序中的捆绑资源。下面是一个在iOS应用程序中使用图像的简单示例。你肯定想考虑只是添加一个手势监听器到你的形象,而不是让它按钮。如果你尝试只使用按钮上的图像,你必须做一些样式调整,使其看起来干净

图像设置

将图像放入iOS资源文件夹

确保已从图像属性中选择bundledresource。

图像按钮

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

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

带有轻触手势的图像

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 
        };
    }
}

一旦部署应用程序,应用程序将无法访问您的mac。要让应用程序使用图片,它需要是iOS应用程序中的捆绑资源。下面是一个在iOS应用程序中使用图像的简单示例。你肯定想考虑只是添加一个手势监听器到你的形象,而不是让它按钮。如果你尝试只使用按钮上的图像,你必须做一些样式调整,使其看起来干净

图像设置

将图像放入iOS资源文件夹

确保已从图像属性中选择bundledresource。

图像按钮

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

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

带有轻触手势的图像

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 
        };
    }
}

图像不在iOS和Android项目中的原因是什么?@Joshua Poling。是的,这张图片来自我mac电脑的照片库。它不在设备上。这就是它不起作用的原因吗?下面的答案解决了你的问题吗?有没有任何原因表明图像不在iOS和Android项目中?@Joshua Poling。是的,这张图片来自我mac电脑的照片库。它不在设备上。这就是它不起作用的原因吗?下面的答案解决了你的问题吗?