简体   繁体   中英

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.

I used this: https://github.com/jamesmontemagno/MediaPlugin

The problem is the functions GetStream() and Dispose() does not exist, here are the exact error messages:

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?)

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?)

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:

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. If you are not up to speed on async and await check out the Microsoft guide at:

https://msdn.microsoft.com/en-us/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.

// file will be a Task<MediaFile>
var file = CrossMedia.Current.PickPhotoAsync();

// file will be a MediaFile
var file = await CrossMedia.Current.PickPhotoAsync();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM