[英]Xamarin.Forms Plugin.Media GetStream/Dispose does not exist
I try to implement a photo picker to get the photos from the Library/Gallery on iOS/Android so i used Plugin.Media with Xamarin.Forms. 我尝试实现一个照片选择器来从iOS / Android上的Library / Gallery获取照片,因此我将Plugin.Media与Xamarin.Forms一起使用。
I used this: https://github.com/jamesmontemagno/MediaPlugin 我用了这个: https : //github.com/jamesmontemagno/MediaPlugin
The problem is the functions GetStream() and Dispose() does not exist, here are the exact error messages: 问题在于函数GetStream()和Dispose()不存在,以下是确切的错误消息:
Error : 'Task' does not contain a definition for 'GetStream' and no extension method 'GetStream' accepting a first argument of type 'Task' could be found (are you missing a using directive or an assembly reference?) 错误 :“任务”不包含“ GetStream”的定义,找不到可以接受类型为“任务”类型的第一个参数的扩展方法“ GetStream”(是否缺少using指令或程序集引用?)
Error : 'Task' does not contain a definition for 'Dispose' and no extension method 'Dispose' accepting a first argument of type 'Task' could be found (are you missing a using directive or an assembly reference?) 错误 :“任务”不包含“处置”的定义,找不到可以接受类型为“任务”类型的第一个参数的扩展方法“处置”(您是否缺少using指令或程序集引用?)
addphotos.Clicked = new Command(() => {
if (CrossMedia.Current.IsPickPhotoSupported)
{
if (!CrossMedia.Current.IsPickPhotoSupported)
{
DisplayAlert("Photos Not Supported", ":( Permission not granted to photos.", "OK");
return;
}
var file = CrossMedia.Current.PickPhotoAsync();
if (file == null)
return;
image.Source = ImageSource.FromStream(() =>
{
var stream = file.GetStream();
file.Dispose();
return stream;
});
}
});
You need to make the lambda async and await the async call to CrossMedia.Current.PickPhotoAsync: 您需要使lambda异步并等待对CrossMedia.Current.PickPhotoAsync的异步调用:
addphotos.Clicked = new Command(async () => {
if (CrossMedia.Current.IsPickPhotoSupported)
{
if (!CrossMedia.Current.IsPickPhotoSupported)
{
DisplayAlert("Photos Not Supported", ":( Permission not granted to photos.", "OK");
return;
}
var file = await CrossMedia.Current.PickPhotoAsync();
if (file == null)
return;
image.Source = ImageSource.FromStream(() =>
{
var stream = file.GetStream();
file.Dispose();
return stream;
});
}
});
PickPhotoAsync() is an async method so it returns a Task, but if you await it it will return the value you are looking for. PickPhotoAsync()是一个异步方法,因此它返回一个Task,但是如果您等待它,它将返回您要查找的值。 If you are not up to speed on async and await check out the Microsoft guide at: 如果您不熟悉异步并等待,请查看Microsoft指南,网址为:
https://msdn.microsoft.com/en-us/library/mt674882.aspx?f=255&MSPPError=-2147217396 https://msdn.microsoft.com/zh-CN/library/mt674882.aspx?f=255&MSPPError=-2147217396
PickPhotoAsync() is, as the name implies, an async function so you need to use await when calling it. 顾名思义,PickPhotoAsync()是一个异步函数,因此您在调用它时需要使用await。
// file will be a Task<MediaFile>
var file = CrossMedia.Current.PickPhotoAsync();
// file will be a MediaFile
var file = await CrossMedia.Current.PickPhotoAsync();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.