不能等待吗?如何等待IAsyncOperation

时间:2019-09-24 10:35:43

标签: c# asynchronous uwp async-await

我真的快要疯了,我无法对下面的异步方法使用任何等待...

这是一个UWP应用程序,下面的代码在基于.NET Standard 2.0的Core项目中。

我已经包含了整个代码,显然还没有完成。最奇怪的行为是BitmapDecoder说没有GetAwaiter(您是否错过了using?),当我删除等待时,它说我需要添加一个等待:D < / p>

using System.Threading.Tasks;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Media.Imaging;
using Windows.Graphics.Imaging;

namespace Namespace...
{
    public class CacheService
    {
        public async Task<BitmapImage> GetCachedImage(long? productId, long? imageId)
        {
            return null;
        }

        public async Task<BitmapImage> SaveImage(byte[] imageBytes, long? productId, long? imageId)
        {
            BitmapImage image = new BitmapImage();

            using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
            {
                using (DataWriter writer = new DataWriter(stream))
                {
                    writer.WriteBytes(imageBytes);
                    writer.StoreAsync();
                    writer.FlushAsync();
                    writer.DetachStream();
                }

                stream.Seek(0);
                image.SetSourceAsync(stream);
                BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
            }

            return image;
        }
    }
}

0 个答案:

没有答案