【问题标题】:How to select an image from gallery in xamarin forms?如何以 xamarin 形式从图库中选择图像?
【发布时间】:2020-12-22 20:57:39
【问题描述】:

我正在尝试从图库中挑选一张图片,并且我已经完成了this doc 中的所有内容,但我的 .cs 实现中的“图片”对象出现错误。

我的 .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;
    }
}

【问题讨论】:

  • 首先,“错误”是什么意思?告诉我们具体错误是什么以及它发生在哪一行将是非常有用的信息。 2、尝试使用 Xamarin Forms 的媒体插件而不是这种方法

标签: c# image xamarin xamarin.forms gallery


【解决方案1】:

欢迎来到 SO!

但我的 .cs 实现中的“图像”对象出现错误。

您可以查看Xaml文件是否包含x:Name="image"

如下:

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

如果不行,可以参考以下步骤:

  • 首先,您需要在这里分享详细的错误日志,并说明发生在 iOS 或 安卓。

  • 其次,您也可以参考这个官方示例Xamarin.Forms - Dependency Service来检查问题出在哪里。

【讨论】:

    猜你喜欢
    • 2018-08-21
    • 1970-01-01
    • 2018-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多