2015-12-18 43 views
2

我需要在执行某段代码之前等待用户确认。这是我建立并显示AlertDialog:等待AlertDialog for Xamarin.Android的用户确认

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”之后。

回答

1

只要把下面的代码在SetPositiveButton回调
dialog.SetPositiveButton("Yes", (sender, args) => { if (cfg.InstantSendVocal) SendCommandToBoard(exactSpeech); }

忘掉所谓userOK

布尔变量