Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.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# 等待用户确认Xamarin.Android的AlertDialog_C#_Xamarin_Xamarin.android - Fatal编程技术网

C# 等待用户确认Xamarin.Android的AlertDialog

C# 等待用户确认Xamarin.Android的AlertDialog,c#,xamarin,xamarin.android,C#,Xamarin,Xamarin.android,在执行某段代码之前,我需要等待用户确认。这是我构建并显示AlertDialog的地方: private bool AskForCommand() { bool userOK = false; AlertDialog.Builder dialog = new AlertDialog.Builder(this); dialog.SetPositiveButton("Yes", (sender, args) => { userOK = true;

在执行某段代码之前,我需要等待用户确认。这是我构建并显示AlertDialog的地方:

private bool AskForCommand()
{
    bool userOK = false;
    AlertDialog.Builder dialog = new AlertDialog.Builder(this);
    dialog.SetPositiveButton("Yes", (sender, args) =>
    {
        userOK = true;
    })
    .SetNegativeButton("No", (sender, args) =>
    {
        userOK = false;
    })
    .SetMessage("Send vocal command now?")
    .SetTitle("System Message");
    RunOnUiThread(() =>
    {
        dialog.Show();
    });

    return userOK;
}
这里是我调用该方法的地方:

if (!string.IsNullOrEmpty(exactSpeech))
{
    bool userOK = false;
    try
    {
        userOK = AskForCommand();
    }
    catch (Exception ex)
    {
        //TODO: catch
    }

    if (cfg.InstantSendVocal || userOK)
        SendCommandToBoard(exactSpeech);
}

问题是,警报显示在方法“SendCommandToBoard”之后。

只需在
SetPositiveButton
callback中的代码下方即可

对话框.SetPositiveButton(“是”,(发送方,参数)=>
{
if(cfg.InstantSendVocal)
发送命令到黑板(exactSpeech);
}

忘记名为userOK的布尔变量吧