如何使用 xamarin.forms 从图库中为 android 和 iOS 设备选择多个图像? [英] How to select multiple images from gallery for android and iOS device using xamarin.forms?

查看:32
本文介绍了如何使用 xamarin.forms 从图库中为 android 和 iOS 设备选择多个图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 xamarin.forms.我正在创建一个可以在 Android 和 iOS 上运行的应用程序.我必须为这两种设备从图库中选择多个图像.

I am working on xamarin.forms. I am creating an app that can run on Android and iOS both. I have to select multiple images from gallery for both devices.

但是,我无法一次选择多个图像.我可以在媒体选择器 (CrossMedia) 的帮助下选择单个图像.

But, I am unable to select multiple images at a time. I can select single image with help of media picker (CrossMedia).

请更新我如何使用 xamarin.forms 从图库中为两台设备选择多个图像?

Please update me how I can select multiple images from gallery for both devices using xamarin.forms?

问候,阿南德·杜比

推荐答案

首先在 Xamarin Forms 中选择多张图片,你应该为每个平台做一个依赖服务.

Firstly for select multiple images in Xamarin Forms, you should do a dependency service for each platform.

在安卓中使用:

.PutExtra (Intent.ExtraAllowMultiple, true);

例如:

  [assembly: Xamarin.Forms.Dependency (typeof (MediaService))]
  namespace MyProject
  {
  public class MediaService : Java.Lang.Object, IMediaService
  {
    public MediaService ()
    {
    }

    public void OpenGallery()
    {

        Toast.MakeText (Xamarin.Forms.Forms.Context, "Select max 20 images", ToastLength.Long).Show ();
        var imageIntent = new Intent(
            Intent.ActionPick);
        imageIntent.SetType ("image/*");
        imageIntent.PutExtra (Intent.ExtraAllowMultiple, true);
        imageIntent.SetAction (Intent.ActionGetContent);
        ((Activity)Forms.Context).StartActivityForResult(
            Intent.CreateChooser (imageIntent, "Select photo"), 0);



       }

     }
   }

在IOS:

Install this control: ELCImagePicker

还有:

[assembly: Xamarin.Forms.Dependency (typeof (MediaService))]
namespace MyProject
{
public class MediaService: IMediaService, IMessanger
{

    public MediaService ()
    {
    }
    public void OpenGallery()
    {

        var picker = ELCImagePickerViewController.Instance;
        picker.MaximumImagesCount = 15;

        picker.Completion.ContinueWith (t => {
            picker.BeginInvokeOnMainThread(()=>
                {
                    //dismiss the picker
                    picker.DismissViewController(true,null);

                    if (t.IsCanceled || t.Exception != null) {
                    } else {
                        Util.File.Path = new List<string>();

                        var items = t.Result as List<AssetResult>;
                        foreach (var item in items) {
                            Util.File.Path.Add (item.Path);
                        }

                        MessagingCenter.Send<IMessanger> (this, "PostProject");
                    }
                });
        });
        var topController = UIApplication.SharedApplication.KeyWindow.RootViewController;
        while (topController.PresentedViewController != null) {
            topController = topController.PresentedViewController;
        }
        topController.PresentViewController (picker, true, null);
    }
  }
}

这篇关于如何使用 xamarin.forms 从图库中为 android 和 iOS 设备选择多个图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆