如何在Xamarin窗体上创建图像选择器?

如何在Xamarin窗体上创建图像选择器?,xamarin,xamarin.forms,plugins,camera,gallery,Xamarin,Xamarin.forms,Plugins,Camera,Gallery,有人知道这样的图像选择器是否可能: 我尝试了以下插件: 我不想创建能够执行每个操作的按钮,我想要的是,通过一个按钮,我建议使用哪个应用程序。这是可能的吗 谢谢。您可以通过xf依赖项服务在Android上使用Intent.SetComponent: Xf: 安卓: [assembly: Dependency(typeof(ShareImage))] namespace AndroidShareSample.Droid { public class ShareImage : Activity

有人知道这样的图像选择器是否可能:

我尝试了以下插件:

我不想创建能够执行每个操作的按钮,我想要的是,通过一个按钮,我建议使用哪个应用程序。这是可能的吗


谢谢。

您可以通过xf依赖项服务在Android上使用Intent.SetComponent:

Xf:

安卓:

[assembly: Dependency(typeof(ShareImage))]
namespace AndroidShareSample.Droid
{
    public class ShareImage : Activity, IShare
    {
        public void Share()
        {
            var appPackageName = "com.sample.app";
            var intent = new Intent(Intent.ActionPick, Android.Provider.MediaStore.Images.Media.ExternalContentUri);
            var resInfos = CrossCurrentActivity.Current.Activity.PackageManager.QueryIntentActivities(intent,0);
            intent.SetComponent(new ComponentName(appPackageName, resInfos.FirstOrDefault(x=> x.ActivityInfo.PackageName == appPackageName).ActivityInfo.Name));
            Forms.Context.StartActivity(Intent.CreateChooser(intent, "Select Image"));
        }
    }
}

由于Xamarin.Forms不包含此功能,如果您想从手机图片库中挑选照片,则必须使用DependencyService访问每个平台上的本机API

创建接口:IPhotoPickerService.cs

public interface IPhotoPickerService
{
    Task<Stream> GetImageStreamAsync();
}
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;
    }
}
Android实现:

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 interface IPhotoPickerService
{
    Task<Stream> GetImageStreamAsync();
}
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;
    }
}
有关IOS、UWP和photo picker实现的更多信息,您可以查看MS文章


并从链接下载源文件

当您在服务中有MainActivity实例时,它将如何作为服务工作?如果您想在应用程序中的多个位置使用此服务,该怎么办?