简体   繁体   中英

Func syntax for async call

I try to create an AsyncCommand with a parameter.

I used this method but it's not working.

 public static AsyncCommand<T> Create(Func<System.Threading.Tasks.Task<T>> func)
    {

        return new AsyncCommand<T>(new Command<T>(async (x) =>  await func(x)));
    }

And when I call it with my viewmodel:

 public ICommand OnRemoveTagCommand = AsyncCommand<ResultElementRatingDto>.Create(RemoveTag);

 private async Task<ResultElementRatingDto> RemoveTag(ResultElementRatingDto ratingDto)
    {

        return null;
    }

The error is:

cannot convert from 'method group' to 'Func'

What's wrong in my code?

Depending on the overloads of Create that are available, and depending on the language version you're using , the compiler sometimes cannot resolve a method group to an unambiguous overload when only considering return types. In that case, call the method with a lambda expression:

public ICommand OnRemoveTagCommand = AsyncCommand<ResultElementRatingDto>.Create(x => RemoveTag(x));

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM