Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 异步方法&x27;匿名';不应返回无效_C#_Asynchronous_Xamarin.forms_Prism_Anonymous Function - Fatal编程技术网

C# 异步方法&x27;匿名';不应返回无效

C# 异步方法&x27;匿名';不应返回无效,c#,asynchronous,xamarin.forms,prism,anonymous-function,C#,Asynchronous,Xamarin.forms,Prism,Anonymous Function,有人能帮我解决这个问题吗?我什么都试过了。 我通常知道如何解决这个问题,但不知道如何使用匿名方法。 DelegateCommand有2个构造函数 1) 公共DelegateCommand(Action executeMethod) 2) 公共DelegateCommand(Action executeMethod,Func canExecute) 我想知道有没有可能删除那个警告。异步和等待是必需的,否则我的方法:enterButtonClicked();将同步调用 ... public

有人能帮我解决这个问题吗?我什么都试过了。 我通常知道如何解决这个问题,但不知道如何使用匿名方法。 DelegateCommand有2个构造函数

1) 公共DelegateCommand(Action executeMethod)

2) 公共DelegateCommand(Action executeMethod,Func canExecute)

我想知道有没有可能删除那个警告。异步和等待是必需的,否则我的方法:enterButtonClicked();将同步调用

 ...
    public DelegateCommand EnterButton { get; set; }

    public StartPageViewModel()
    {
        Title = "title_black.png";
        PasswordPlaceholder = "Lozinka";

        EnterButton = new DelegateCommand( async () => { await enterButtonClicked();}); // <----- I am getting that warning here
    }

    public async Task enterButtonClicked()
    {

    }
...
。。。
公共DelegateCommand EnterButton{get;set;}
公共StartPageViewModel()
{
Title=“Title\u black.png”;
PasswordPlaceholder=“Lozinka”;

EnterButton=newdelegateCommand(async()=>{await enterButtonClicked();});//async await仅与
Func
Func
兼容,如果您没有该命令,那么您就拥有了不应该执行的“异步无效”

你的两个选择是不要等待任务

...
public DelegateCommand EnterButton { get; set; }

public StartPageViewModel()
{
    Title = "title_black.png";
    PasswordPlaceholder = "Lozinka";

    EnterButton = new DelegateCommand( () => { var temp = enterButtonClicked();}); 
}

public async Task enterButtonClicked()
{

}
...
这意味着enterButtonClicked引发的任何异常都不会被注意到

或者使用支持异步函数的更好的委托命令


当任务运行时,
AsyncCommand
委托将
CanExecute
设置为false,这样,除非操作完成,否则用户不能重复单击。

您需要向我们显示DelegateCommand构造函数的签名。很可能,您正在创建“async void”函数,因为DelegateCommand不包含接受
Func
的构造函数。只需执行
新建DelegateCommand(enterButtonClicked)
,问题应该更加明显。它有两个构造函数1)公共DelegateCommand(Action executeMethod)和2)公共DelegateCommand(Action executeMethod,Func canExecute)。我想知道是否有可能删除该警告。需要async和Wait,否则将同步调用我的方法:enterButtonClicked();非常感谢您提供的非常好的解释。
...
public AsyncCommand EnterButton { get; set; }

public StartPageViewModel()
{
    Title = "title_black.png";
    PasswordPlaceholder = "Lozinka";

    EnterButton = new DelegateCommand(enterButtonClicked); //you can just use a delegate, no method needed.
}

public async Task enterButtonClicked()
{

}
...