【问题标题】:How to select multiple images from gallery for android and iOS device using xamarin.forms?如何使用 xamarin.forms 从图库中为 android 和 iOS 设备选择多个图像?
【发布时间】:2016-02-18 10:05:23
【问题描述】:

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

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

请告诉我如何使用 xamarin.forms 从两个设备的图库中选择多个图像?

问候, 阿南德·杜贝

【问题讨论】:

  • 您需要修改 CrossMedia(其源代码在 GitHub 上)或自己编写。我认为没有任何现有的代码可以为您开箱即用。
  • 这意味着xamarin.forms中没有可以从图库中选择多个图像的特定控件?
  • 我不知道
  • 感谢您的回复。你能更新我的 github 的 url,在那里我可以得到 CrossMedia 的代码吗?

标签: xamarin.forms


【解决方案1】:

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

在 Android 中使用:

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

【讨论】:

  • Android 示例中如何获取照片?
  • ExtraAllowMultiple 仅支持 API 18 及以上版本
  • ELCImagepicker 在 NuGet 商店中找不到了,还有什么其他选项?
猜你喜欢
  • 2016-03-05
  • 2018-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多