I have an asynchronous method in C# class
namespace ImageHolder
{
public sealed class BlankFilter
public async Task<InMemoryRandomAccessStream> applyFilter()
{
inputStream = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(inputStream);
/* more code */
}
}
I am calling this function from a WinJS class in the following way
filter.applyFilter().then(function (memStream) {
var msStream = MSApp.createStreamFromInputStream("image/jpeg", memStream);
var imageURL = URL.createObjectURL(msSteam);
id("imageInput").src = imageURL;
});
However this is not working and Visual Studio complains that the return type should be changed to IAsyncAction. Doing that is also not helping.
So, what is the correct way to call a C# function that has await from WinJS code?
WinJS cannot call methods which return
Task<T>– which is what you get with an async/await method. As the error say, you need to change the return type (or create a new, wrapper method) toIAsyncOperation<T>. Converting fromTask<T>toIAsyncOperation<T>is simple (call eitherAsAsyncOperationon the resulting task).This blog post has more information about this issue.