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

查看:337
本文介绍了如何使用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?

关于, 阿南德·杜比(Anand Dubey)

Regards, Anand Dubey

推荐答案

首先要在Xamarin Forms中选择多个图像,应为每个平台进行依赖服务.

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

在Android中使用:

In Android use:

.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天全站免登陆