I have a function :
public async Task DoStuff()
{
await DoSomethingAsync();
}
I need to call it from code running this:
list.ItemTapped += async (sender, e) =>
{
file.WriteAllBytes(reportPdf, data, () => **await** file.DoStuff());
};
But the await
is not allowed in the Action file.DoStuff
called in the above???
While GazTheDestroyer's answer is correct, there's no reason for the lambda expression to be marked async
and to use an await
since file.DoStuff
already returns a Task
and fits the Func<Task>
delegate:
list.ItemTapped += async (sender, e) =>
{
file.WriteAllBytes(reportPdf, data, () => file.DoStuff());
};
It would also slightly improve performance since there's no reason to construct the state machine required for an async-await
method/delegate.
Your second lambda is not marked async
list.ItemTapped += async (sender, e) =>
{
file.WriteAllBytes(reportPdf, data, async () => await file.DoStuff());
};
This depends on file.WriteAllBytes()
allowing a Func<Task>
to be passed in. An Action
will not be enough.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.