C# 等待用户确认Xamarin.Android的AlertDialog
在执行某段代码之前,我需要等待用户确认。这是我构建并显示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;
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的布尔变量吧