如何在Xamarin Forms上创建图像选择器? [英] How to create a image picker on Xamarin Forms?
问题描述
有人知道是否可以制作像这样的图像选择器:
Does anybody knows if its possible to make a Image picker like this:
我尝试使用以下插件:
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?
谢谢.
推荐答案
如果由于Xamarin.Forms不包含此功能而要从手机的图片库中选择一张照片,则必须使用DependencyService
访问每个平台上的本机API.
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.
创建界面:IPhotoPickerService.cs
Create the interface: IPhotoPickerService.cs
public interface IPhotoPickerService
{
Task<Stream> GetImageStreamAsync();
}
Android实现:
MainActivity.cs
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
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;
}
}
有关IOS,照片选择器的UWP实现的更多信息,您可以查看MS文章. https://docs. microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/photo-picker
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
然后从链接下载源文件. https://docs.microsoft.com/zh -cn/samples/xamarin/xamarin-forms-samples/dependencyservice/
And download the source file from link. https://docs.microsoft.com/zh-cn/samples/xamarin/xamarin-forms-samples/dependencyservice/
这篇关于如何在Xamarin Forms上创建图像选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!