-
Notifications
You must be signed in to change notification settings - Fork 267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for async/await handler on Register handler in Messenger #332
Comments
related to #331 |
This is already supported, you're just registering the callback incorrectly. You can do this: WeakReferenceMessenger.Default.Register<ContinueCallbackAnsweringRequestMessage>(this, (r, m) =>
{
async Task<Foo> ReceiveAsync()
{
await FooAsync();
await BarAsync();
return foo;
}
m.Reply(ReceiveAsync());
}); I should note, you should change this to avoid creating closures, for better performance (see docs): WeakReferenceMessenger.Default.Register<MyViewModel, ContinueCallbackAnsweringRequestMessage>(this, static (r, m) =>
{
async Task<Foo> ReceiveAsync(MyViewModel r)
{
await r.FooAsync();
await r.BarAsync();
return r.GetSomeFoo();
}
m.Reply(ReceiveAsync(r));
}); We might consider adding some helper extensions to support this scenario in an easier way in the future I guess 🤔 |
@Sergio0694 very thank you for response the minor note is I think your code is not compile, because |
I have to use C# 7.3 and cannot use WeakReferenceMessenger.Default.Register<Dialer, ContinueCallbackAnsweringRequestMessage>(this, (r, message) =>
{
async Task<Task<CancellationToken>> ReceiveAsync(Dialer r)
{
var continueCallbackAnsweringDialog = new ContinueCallbackAnsweringDialog();
await continueCallbackAnsweringDialog.ShowAsync();
return continueCallbackAnsweringDialog.ResultAsync;
}
message.Reply(ReceiveAsync(r).Unwrap());
}); |
Using the |
After I changing my code from your suggestion, I got Am I use thanks |
I found my problem, thanks :) |
Overview
ITNOA
I think it is good to can call asynchronous method on handler of
Register
method ofMessenger
andawait
on that without problem.API breakdown
No API added.
Usage example
Breaking change?
No
Alternatives
I do not found any alternative :(
Additional context
No response
Help us help you
Yes, but only if others can assist
The text was updated successfully, but these errors were encountered: