有人知道是否可以制作像这样的图像选择器:
我尝试使用以下插件:
https://github.com/jamesmontemagno/MediaPlugin
https://github.com/matheusneder/Xamarin.Forms.ImagePicker
我不想创建能够执行每个操作的按钮,我想要的是使用单个按钮来建议使用哪个应用程序。这可能吗?
谢谢。
有人知道是否可以制作像这样的图像选择器:
我尝试使用以下插件:
https://github.com/jamesmontemagno/MediaPlugin
https://github.com/matheusneder/Xamarin.Forms.ImagePicker
我不想创建能够执行每个操作的按钮,我想要的是使用单个按钮来建议使用哪个应用程序。这可能吗?
谢谢。
您可以通过xf依赖项服务在Android上使用Intent.SetComponent:
Xf:
y
Android:
x
,
由于Xamarin.Forms不包含此功能,因此如果要从手机的图片库中选择照片,则必须使用DependencyService
来访问每个平台上的本机API。
创建界面:IPhotoPickerService.cs
public interface IPhotoPickerService
{
Task<Stream> GetImageStreamAsync();
}
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 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
然后从链接下载源文件。 https://docs.microsoft.com/zh-cn/samples/xamarin/xamarin-forms-samples/dependencyservice/