要关闭对话框

时间:2016-12-17 11:06:01

标签: android xamarin dialog

我写了这段代码:

            button.Click += (s, e) =>
            {
                string strDialog = "Hello";
                Dialog dialog = new Dialog(this);
                dialog.Window.RequestFeature(WindowFeatures.NoTitle);
                dialog.SetContentView(Resource.Layout.SDialog);
                TextView text = (TextView)dialog.FindViewById(Resource.Id.dialog_text_view);
                text.Text = strDialog;
(Button)dialog.FindViewById(Resource.Id.dismiss_dialog_button);
                dialogButton.Click += delegate
                {
                    dialog.Dismiss();
                };
                dialog.Show();
            };

单击“按钮”时,会打开对话框。当我不关闭对话框并再次单击“按钮”时,对话框将关闭。在这种情况下,对话被驳回了吗?

1 个答案:

答案 0 :(得分:1)

private void Button_Click(object sender, System.EventArgs e)
{
    string strDialog = "Hello";
    Dialog dialog = new Dialog(this);
    // dialog.SetCanceledOnTouchOutside(false);
    dialog.Window.RequestFeature(WindowFeatures.NoTitle);
    dialog.SetContentView(Resource.Layout.DialogView);
    dialog.DismissEvent += Dialog_DismissEvent;
    dialog.CancelEvent += Dialog_CancelEvent;
    TextView text = (TextView)dialog.FindViewById(Resource.Id.MyTextView);
    text.Text = strDialog;
    var dialogButton = (Button)dialog.FindViewById(Resource.Id.DismissBtn);
    dialogButton.Click += delegate
    {
        dialog.Dismiss();
    };
    dialog.Show();

}

private void Dialog_CancelEvent(object sender, System.EventArgs e)
{
    //cancel
}

private void Dialog_DismissEvent(object sender, System.EventArgs e)
{
    //dismiss
}

我在Dialog_CancelEventDialog_DismissEvent设置了两个断点。首先调用Dialog_CancelEvent,然后在触摸窗口界限被解除对话框时调用Dialog_DismissEvent。如果通过单击DismissBtn关闭对话框,则只调用Dialog_DismissEvent。对话被解雇了。

如果您不希望在单击“对话框”外部时关闭对话框,则可以使用dialog.SetCanceledOnTouchOutside(false)