Reputation: 13
Getting error:
Error CS4010: Cannot convert async lambda expression to delegate type 'Func'. An async lambda expression may return void, Task or Task, none of which are convertible to 'Func
UIAccessibilityCustomAction someAccessibilityAction = new
UIAccessibilityCustomAction(
SwipeActionMarkTextValue("Accessibility Text", false),
async (UIAccessibilityCustomAction arg) =>{
await TestAsyncMethod();
return true;
});
private async Task<bool> TestAsyncMethod()
{
await Task.Delay(5000);
return true;
}
Upvotes: 0
Views: 2707
Reputation: 3688
Remember that async is (kindof) syntactic sugar for ContinueWith. Your syntax is perfect but it helps to illustrate my point if I rewrite it as:
(UIAccessibilityCustomAction arg) => TestAsyncMethod().ContinueWith(b => true)
I am not familiar with Xamarin.iOS, and it also depends on the task you want to achieve but here are some general solutions:
Wait (block) on your work item:
/*NO ASYNC HERE*/ (UIAccessibilityCustomAction arg) =>{
TestAsyncMethod().GetAwaiter().GetResult();
return true;
})
Depending on your situation this might cause a deadlock though.
Make your worker "fire and forget" (async void) and immediately return true after kicking it off.
UIAccessibilityCustomAction someAccessibilityAction = new UIAccessibilityCustomAction(
SwipeActionMarkTextValue("Accessibility Text", false),
(UIAccessibilityCustomAction arg) => {
TestAsyncMethod();
return true;
});
private async void TestAsyncMethod()
{
await Task.Delay(5000);
}
Upvotes: 2