本文介绍了如何在 Xamarin Forms 上创建图像选择器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道是否可以制作这样的图像选择器:

我尝试过以下插件:

关于IOS、UWP实现照片选择器的更多信息,你可以查看MS文章.https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/photo-picker

并从链接下载源文件.https://docs.microsoft.com/zh-cn/samples/xamarin/xamarin-forms-samples/dependencyservice/

Does anybody knows if its possible to make a Image picker like this:

I've tried with the followings plugins :

https://github.com/jamesmontemagno/MediaPlugin

https://github.com/matheusneder/Xamarin.Forms.ImagePicker

I do not want to create buttons to be able to perform each operation, what I want is that with a single button I suggest which application to use. This is possible?

Thanks.

解决方案

If you want to pick a photo from the phone's picture library, due to Xamarin.Forms does not include this functionality, it is necessary to use DependencyService to access native APIs on each platform.

Create the interface: IPhotoPickerService.cs

public interface IPhotoPickerService
{
    Task<Stream> GetImageStreamAsync();
}

Android implementation:

MainActivity.cs

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    internal static MainActivity Instance { get; private set; }

      ... ...

    // Field, property, and method for Picture Picker
    public static readonly int PickImageId = 1000;

    public TaskCompletionSource<Stream> PickImageTaskCompletionSource { set; get; }

    protected override void OnActivityResult(int requestCode, Result resultCode, Intent intent)
    {
        base.OnActivityResult(requestCode, resultCode, intent);

        if (requestCode == PickImageId)
        {
            if ((resultCode == Result.Ok) && (intent != null))
            {
                Android.Net.Uri uri = intent.Data;
                Stream stream = ContentResolver.OpenInputStream(uri);

                // Set the Stream as the completion of the Task
                PickImageTaskCompletionSource.SetResult(stream);
            }
            else
            {
                PickImageTaskCompletionSource.SetResult(null);
            }
        }
    }
}

PhotoPickerService.cs

public class PhotoPickerService : IPhotoPickerService
{
    public Task<Stream> GetImageStreamAsync()
    {
        // Define the Intent for getting images
        Intent intent = new Intent();
        intent.SetType("image/*");
        intent.SetAction(Intent.ActionGetContent);

        // Start the picture-picker activity (resumes in MainActivity.cs)
        MainActivity.Instance.StartActivityForResult(
            Intent.CreateChooser(intent, "Select Photo"),
            MainActivity.PickImageId);

        // Save the TaskCompletionSource object as a MainActivity property
        MainActivity.Instance.PickImageTaskCompletionSource = new TaskCompletionSource<Stream>();

        // Return Task object
        return MainActivity.Instance.PickImageTaskCompletionSource.Task;
    }
}

For more information about IOS, UWP implementation of photo picker, you could check the MS article. https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/photo-picker

And download the source file from link. https://docs.microsoft.com/zh-cn/samples/xamarin/xamarin-forms-samples/dependencyservice/

这篇关于如何在 Xamarin Forms 上创建图像选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 20:26
查看更多