使用co_await时编译错误

时间:2018-02-02 12:13:54

标签: uwp c++-cx

为什么当我构建自己的自定义类时,我会收到以下错误?

部首:

MapRoute

实现:

namespace PSImageLibrary {
    public ref class PSImage sealed {

    public:
        PSImage();
        virtual ~PSImage();

        IAsyncAction^open(StorageFile^ file);

    private:
        IRandomAccessStream^ stream;
        BitmapDecoder^ decoder;
        PixelDataProvider^ provider;
        BitmapTransform^ transform;

        IAsyncAction^_decode(IRandomAccessStream^);
    };
}

我的例外发生在这里:

PSImageLibrary::PSImage::PSImage() {
    transform = ref new BitmapTransform();
}

PSImageLibrary::PSImage::~PSImage() { }

IAsyncAction^ PSImageLibrary::PSImage::open(StorageFile^ file) {
    return create_async([this, file] {
        return create_async([this, file]() -> task<void> {
            stream = co_await file->OpenAsync(FileAccessMode::Read);
            co_await _decode(stream);
        });
    });
}

IAsyncAction^ PSImageLibrary::PSImage::_decode(IRandomAccessStream^ stream) {
    return create_async([this, stream] {
        return create_async([this, stream]() -> task<void> {
            decoder = co_await BitmapDecoder::CreateAsync(stream);
        });
    });
}

我认为当我使用file-&gt; OpenAsync进行流式处理时会出现错误,但我不知道为什么会出现错误。我该如何解决?

1 个答案:

答案 0 :(得分:1)

这是您正在编译的Windows运行时组件吗?他们不能将任务包含为公共返回值 - 您希望返回IAsyncAction^

您可以使用create_async执行此操作,此处提供了一些示例:

https://docs.microsoft.com/en-gb/windows/uwp/winrt-components/walkthrough-creating-a-basic-windows-runtime-component-in-cpp-and-calling-it-from-javascript-or-csharp

在您的情况下,可能类似于以下内容:

IAsyncAction^ PSImageLibrary::PSImage::open(StorageFile^ file) {
    return create_async([file]
    {
        stream = co_await file->OpenAsync(FileAccessMode::Read);    
        _decode();
    });
}