如何实现Xamarin Android从AlertDialog返回响应bool?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (166)

以下是我的代码:

public bool AskForConfirmation(string messege, Context context)
{
    bool result;

    Android.Support.V7.App.AlertDialog.Builder dialog = new Android.Support.V7.App.AlertDialog.Builder(context);

    dialog.SetPositiveButton("Yes", (sender, args) =>
    {
        result = true;
    });

    dialog.SetNegativeButton("No", (sender, args) =>
    {
        result = false;
    }).SetMessage(messege).SetTitle("System Message");


    dialog.Show();

    return result;
}
this.RunOnUiThread(() =>
{

    bool response = ioManager.AskForConfirmation("Message", this);

    Console.WriteLine("Response is " + response);

});
提问于
用户回答回答于

可以通过ManualResetEvent创建一个Task:

using (var cancellationTokenSource = new CancellationTokenSource())
{
    var result = await DialogAsync.Show(this, "StackOverflow", "Does it rock?", cancellationTokenSource);
    if (!cancellationTokenSource.Token.IsCancellationRequested)
    {
        Log.Debug("SO", $"Dialog result: {result}");
    }
    else
    {
        Log.Debug("SO", $"Dialog cancelled; backbutton, click outside dialog, system-initiated, .... ");
    }
}

DialogAsync示例:

public class DialogAsync : Java.Lang.Object, IDialogInterfaceOnClickListener, IDialogInterfaceOnCancelListener
{
    readonly ManualResetEvent resetEvent = new ManualResetEvent(false);
    CancellationTokenSource cancellationTokenSource;
    bool? result;

    public DialogAsync(IntPtr handle, Android.Runtime.JniHandleOwnership transfer) : base(handle, transfer) { }
    public DialogAsync() { }

    public void OnClick(IDialogInterface dialog, int which)
    {
        switch (which)
        {
            case -1:
                SetResult(true);
                break;
            default:
                SetResult(false);
                break;
        }
    }

    public void OnCancel(IDialogInterface dialog)
    {
        cancellationTokenSource.Cancel();
        SetResult(null);
    }

    void SetResult(bool? selection)
    {
        result = selection;
        resetEvent.Set();
    }

    public async static Task<bool?> Show(Activity context, string title, string message, CancellationTokenSource source)
    {
        using (var listener = new DialogAsync())
        using (var dialog = new AlertDialog.Builder(context)
                                                            .SetPositiveButton("Yes", listener)
                                                            .SetNegativeButton("No", listener)
                                                            .SetOnCancelListener(listener)
                                                            .SetTitle(title)
                                                            .SetMessage(message))
        {
            listener.cancellationTokenSource = source;
            context.RunOnUiThread(() => { dialog.Show(); });
            await Task.Run(() => { listener.resetEvent.WaitOne(); }, source.Token);
            return listener.result;
        }
    }
}

扫码关注云+社区

领取腾讯云代金券