1

I'm really going crazy, I'm not able to use any awaits for the async methods below...

It's a UWP application, the code below is inside a Core project based on .NET Standard 2.0.

I've included the entire code, it's obviously unfinished. The most odd behavior is the BitmapDecoder says there is no GetAwaiter (are you missing a using?), when I remove the await, it says I need to add an await :D

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;
        }
    }
}
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
SparkyRih
  • 49
  • 9
  • 5
    You need some stuff to await an IAsyncOperation: https://stackoverflow.com/questions/44099401/frombluetoothaddressasync-iasyncoperation-does-not-contain-a-definition-for-get/44102996#44102996 I'm not sure of what you need when using netstandard (I've stopped using UWP long ago), but that should be a good start – Kevin Gosse Sep 24 '19 at 10:37
  • @KevinGosse Thanks, that helped. The application is for my own business, I thought to try UWP for this one (since it is a small application, I can easily convert it to a different platform if needed)… Why did you stop using UWP? – SparkyRih Sep 24 '19 at 11:07
  • 1
    This went downhill because you used a UWP-specific class in a .NETStandard project. BitmapDecoder is not .netstandard, fairly mysterious how you got it to compile. Since the library can never be standard you need to get ahead by using the right project template. Its name is "Class Library (Universal Windows)". – Hans Passant Sep 24 '19 at 11:07
  • @SparkyRih Not for reasons related to the technology itself. It's just that I haven't had to write client apps in a long time :p – Kevin Gosse Sep 24 '19 at 11:08
  • Hi, like @SparkyRih said, you need to create a `Class Library (Universal Windows)`, not the `Class Library (.Net Standard)`. – Richard Zhang Sep 24 '19 at 11:54

0 Answers0