【发布时间】:2018-01-02 22:08:56
【问题描述】:
我正在使用 async 和 await 来获取按钮的流。 async 方法工作正常,当我使用 await 时,它返回函数。所以我总是在内存流中获取 null 值。
当我使用这个 (await renderTargetBitmap.RenderAsync(button)) 操作意味着它返回语句而不进行计算。
任何人,请建议我如何解决这个问题?我需要添加任何其他代码吗?
这是我的代码:
public class StreamDependencyService: IStreamDependency
{
Button button;
public Stream GetStream()
{
button = new Button() { Height = 50, Width = 50, Content = "Click" };
return GetStream_UWP().Result;
}
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;
}
}
【问题讨论】:
标签: uwp xamarin.forms cross-platform