[UWP]How to use async and await methods in Xamarin.UWP

Hi,

I want to get the stream of the object. I tried the below code, but is not working. When I call the async method, it returns the value. memoryStream value always null.

Code:

public async Task<Stream> GetStream_UWP()
{
    Stream memoryStream ;
    var renderTargetBitmap = new RenderTargetBitmap();
    await renderTargetBitmap.RenderAsync(button);
    var pixels = await renderTargetBitmap.GetPixelsAsync();
    Guid encoderId = BitmapEncoder.JpegEncoderId;
    using (memoryStream = new MemoryStream())
    {
        var encoder = await BitmapEncoder.CreateAsync(encoderId, memoryStream.AsRandomAccessStream());
        encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)renderTargetBitmap.PixelWidth,
            (uint)renderTargetBitmap.PixelHeight, 96.0, 96.0, pixels.ToArray());

        await encoder.FlushAsync();

    }
    return memoryStream;
}

Please suggest me.

Answers

  • kentuckerkentucker ✭✭✭✭✭ USMember ✭✭✭✭✭

    I dont see where you actually get any pixel data to set.

Sign In or Register to comment.