异步调用的功能语法

问题描述 投票:1回答:2

我尝试使用参数创建AsyncCommand。

我使用了这种方法,但是不起作用。

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

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

当我用我的视图模型调用它时:

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

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

        return null;
    }

错误是:

无法从“方法组”转换为“功能”

我的代码有什么问题?

c# multithreading xamarin xamarin.forms func
2个回答
2
投票

取决于可用的Create的重载,并且取决于language version you're using,有时仅考虑返回类型时,编译器有时无法将方法组解析为明确的重载。在这种情况下,请使用lambda表达式调用该方法:


0
投票

完美。非常感谢您的回答。

© www.soinside.com 2019 - 2024. All rights reserved.