简体   繁体   English

如何以 xamarin 形式从图库中选择图像?

[英]How to select an image from gallery in xamarin forms?

I'm trying to pick an image from gallery and i've done everything that is in this doc, but i'm getting an error with the "image" object in my .cs implementation.我正在尝试从图库中选择一个图像,并且我已经完成了文档中的所有内容,但是我的 .cs 实现中的“图像”对象出现错误。

My .cs code:我的 .cs 代码:

 public partial class MyProfilePage : ContentPage
{
  
    public MyProfilePage()
    {
        InitializeComponent();
        BindingContext = new MyProfileViewModel();
    }

   
    async void ImageButton_Clicked(object sender, EventArgs e)
    {
        //(sender as Button).IsEnabled = true;

        Stream stream = await DependencyService.Get<IPhotoPickerService>().GetImageStreamAsync();
        if (stream != null)
        {
            
            image = ImageSource.FromStream(() => stream);
        }

        (sender as Button).IsEnabled = true;
    }
}

Welcome to SO!欢迎来到 SO!

but i'm getting an error with the "image" object in my .cs implementation.但是我的 .cs 实现中的“图像”对象出现错误。

You can check the Xaml file that whether contain the x:Name="image" .您可以检查Xaml文件是否包含x:Name="image"

Such as follows:例如如下:

<ContentPage Title="Photo Picker"
             Icon="monkeyicon.png">
    <StackLayout Margin="20,35,20,20" >
        <Label Text="Photo Picker"
               FontAttributes="Bold"
               HorizontalOptions="Center" />
        <Button Text="Pick Photo"
                Clicked="OnPickPhotoButtonClicked" />
        <Image x:Name="image" />
    </StackLayout>        
</ContentPage>

If not works , you can refer to following steps:如果不行,可以参考以下步骤:

  • First , you need to share the detail error logs here, and explain this occurs in iOS or Android.首先,您需要在这里分享详细的错误日志,并解释在 iOS 或 Android 中发生的情况。

  • Second , you also can refer to this official sample Xamarin.Forms - Dependency Service to check where problem is.其次,您也可以参考这个官方示例Xamarin.Forms - Dependency Service来检查问题出在哪里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM