【问题标题】:Await user confirmation for AlertDialog for Xamarin.Android等待用户确认 Xamarin.Android 的 AlertDialog
【发布时间】:2015-12-18 11:25:07
【问题描述】:

在执行某段代码之前,我需要等待用户确认。这是我构建和显示 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”之后。

【问题讨论】:

    标签: c# xamarin xamarin.android


    【解决方案1】:

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

    忘记名为 userOK 的布尔变量

    【讨论】:

      猜你喜欢
      • 2018-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多