按下中性按钮时阻止Android AlertDialog关闭

时间:2016-01-20 14:57:58

标签: c# android xamarin dialog

我正在使用来自Nuget的Xamarin和V7支持库。

我想覆盖默认的Dialog.Dismiss()行为。我没有运气试过这个,当按下空档(刷新)按钮时对话框关闭:

为清晰起见,

更新:我需要的行为是按下空档按钮后MyDialog仍保持打开状态。目前,当按下中性按钮时,refreshMyDialogButton_Click方法将触发,然后MyDialog将自动被解除。

using AlertDialog = Android.Support.V7.App.AlertDialog;

....
protected AlertDialog MyDialog;

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    var myDialogView = View.Inflate(this, Resource.Layout.myDialogView, null);
    MyDialog = createMyDialog(myDialogView);
    MyDialog.Show();
}

protected AlertDialog createMyDialog(View myDialogView)
{
    var builder = new AlertDialog.Builder(this);
    builder
        .SetNegativeButton("back", delegate { MyDialog.Dismiss(); })
        .SetNeutralButton("refresh", (EventHandler<DialogClickEventArgs>)refreshMyDialogButton_Click)
        .SetPositiveButton(/**foo**/)
        .SetView(/**my custom view**/)
        .SetTitle(/**title**/)
        .SetMessage(/**message**/)
        ;
    return builder.Create();
}

private void refreshMyDialogButton_Click(object sender, EventArgs e)
{
    /** start async tasks but don't close dialog **/
}

更新2

我在onCreate方法中添加了以下代码,但这导致了dialogNeutral上的空引用异常:

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    var myDialogView = View.Inflate(this, Resource.Layout.myDialogView, null);
    MyDialog = createGathererDialog(myDialogView);
    var dialogNeutral = MyDialog.GetButton((int)DialogButtonType.Neutral);
    dialogNeutral.Click += refreshMyDialogButton_Click;
}

protected AlertDialog createMyDialog(View myDialogView)
{
    var builder = new AlertDialog.Builder(this);
    builder
        .SetNegativeButton("back", delegate { MyDialog.Dismiss(); })
        .SetNeutralButton("refresh", (EventHandler<DialogClickEventArgs>)null)
        .SetPositiveButton(/**foo**/)
        .SetView(/**my custom view**/)
        .SetTitle(/**title**/)
        .SetMessage(/**message**/)
        ;
    return builder.Create();
}

2 个答案:

答案 0 :(得分:0)

必须将按钮实例化为null,然后在调用.GetButton()方法后通过.Show()进行访问。

答案 1 :(得分:0)

我知道我对这个答案迟到了,但你可以在对话课中尝试这个。

this.Dialog.SetCanceledOnTouchOutside(false);

从外面访问时,

dialogBox.Dialog.SetCanceledOnTouchOutside(false);