简体   繁体   English

Xamarin.Forms Plugin.Media GetStream / Dispose不存在

[英]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.

相关问题 Xamarin Plugin.Media “无法获取文件位置” - Xamarin Plugin.Media “Unable to get file location” 名称空间xamarin.forms中不存在名称空间名称Masterdetailpage的类型 - type of namespace name Masterdetailpage does not exist in the namespace xamarin.forms 当前上下文中不存在名称“HapticFeedback” - Xamarin.Forms - The name 'HapticFeedback' does not exist in the current context - Xamarin.Forms 升级到 Xamarin.Forms 后类型支持不存在 5 - Type Support does not exist after upgrade to Xamarin.Forms 5 如何使用媒体插件在 Xamarin.Forms 中实现相机叠加的依赖服务? - How to Implement Dependency Service for Camera Overlay in Xamarin.Forms Using the Media Plugin? Xam.Plugin.Media的方法“ TakePhotoAsync”在Xamarin.Forms的平台WinPhone上返回null - Method “TakePhotoAsync” of Xam.Plugin.Media return null on platform WinPhone in Xamarin.Forms 如何使用Xamarin.Forms的媒体插件在共享代码中实现iOS覆盖? - How to Implement an iOS Overlay in Shared Code Using the Media Plugin for Xamarin.Forms? 在Xamarin.Forms中处理WebView页面的正确方法是什么? - What is the right way to dispose a WebView page in Xamarin.Forms? Xamarin.Forms jamesmontemagno设置插件 - Xamarin.Forms jamesmontemagno settings plugin Xamarin.Forms Windows 10通用错误“CS0103 InitializeComponent不存在” - Xamarin.Forms Windows 10 Universal Error “CS0103 InitializeComponent does not exist”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM