So far I could make a delegate type, for example:
// Can't use Task in WinRT interface and TypedEventHandler doesn't work with async await
public delegate IAsyncOperation<string> AsyncEventHandler(object sender, object args);
And then expose in WinRT object like so:
public AsyncEventHandler OnMyEvent { get; set; }
In the WinRT object I would call it like this:
if (OnMyEvent != null)
{
var result = await OnMyEvent.Invoke(this, someArgs);
// Do something with the result...
}
And the client code consuming the WinRT object could do this:
instanceOfWinRTObject.OnMyEvent = OnCalledBackFromWinRT;
But because the delegate returns an IAsyncOperation
we need to do some wrapping:
private async Task<string> OnCalledBackFromWinRTAsync(object sender,
object args)
{
return await GetSomeStringAsync(args);
}
private IAsyncOperation<string> OnCalledBackFromWinRT(object sender, object args)
{
return OnCalledBackFromWinRTAsync(sender, args).AsAsyncOperation();
}
It just feels like there must be a cleaner way to achieve this.