Xamarin C# - Android的 - 从收盘PositiveButton点击防止AlertDialog [英] Xamarin C# - Android - Prevent an AlertDialog from closing on PositiveButton click

查看:222
本文介绍了Xamarin C# - Android的 - 从收盘PositiveButton点击防止AlertDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来Xamarin,我不知道该怎么做在C#以下。我想防止alertdialog从正/负按钮,当点击关闭。我需要做一些验证输入第一。如果输入的是正确的,对话可以关闭,否则我将展示说明的邮件。基本上,我有以下代码:

I'm new to Xamarin and I don't know how to do the following in c#. I want to prevent an alertdialog from closing when clicking on the Positive/Negative buttons. I need to do some validation on the input first. If the input is correct, the dialog can close, else I will show a message with instructions. Basically, I have the following code:

private void CreateAddProjectDialog() { 
    //some code
    var alert = new AlertDialog.Builder (this);
    alert.SetTitle ("Create new project");
    alert.SetView (layoutProperties);
    alert.SetCancelable (false);
    alert.SetPositiveButton("Create", HandlePositiveButtonClick);
    alert.SetNegativeButton("Cancel", HandelNegativeButtonClick);
}

private void HandlePositiveButtonClick (object sender, EventArgs e) {
    //Do some validation here and return false (prevent closing of dialog) if invalid, else close....
}

现在,我上冲计算器以下职位:的How防止关闭一个对话框单击按钮时

Now, I red the following post on StackOverflow: How to prevent a dialog from closing when a button is clicked

我觉得下面的代码(从线程所)的解决方案,但我不知道如何重写我的C#代码来实现Java

I think the code below (taken from the thread) has the solution, but I don't know how to rewrite my c# code to implement the Java:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Test for preventing dialog close");
builder.setPositiveButton("Test", 
    new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick(DialogInterface dialog, int which)
        {
            //Do nothing here because we override this button later to change the close behaviour. 
            //However, we still need this because on older versions of Android unless we 
            //pass a handler the button doesn't get instantiated
        }
    });
AlertDialog dialog = builder.create();
dialog.show();
//Overriding the handler immediately after show is probably a better approach than     OnShowListener as described below
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener()
{

    @Override
    public void onClick(View v)
    {
        Boolean wantToCloseDialog = false;
        //Do stuff, possibly set wantToCloseDialog to true then...
        if(wantToCloseDialog)
                dismiss();
        //else dialog stays open. Make sure you have an obvious way to close the dialog especially if you set cancellable to false.
    }
});



如何在C#代码呢?特别是在setPositiveButton区域覆盖的部分...

How to code this in c#? Especially the override part in the setPositiveButton area...

推荐答案

这要求条条框框一点。你将不得不直接处理 AlertDialog 目标:

This requires to think a bit outside the box. You will have to manipulate the AlertDialog object directly:

// Build the dialog.
var builder = new AlertDialog.Builder(this);
builder.SetTitle("Click me!");

// Create empty event handlers, we will override them manually instead of letting the builder handling the clicks.
builder.SetPositiveButton("Yes", (EventHandler<DialogClickEventArgs>)null);
builder.SetNegativeButton("No", (EventHandler<DialogClickEventArgs>)null);
var dialog = builder.Create();

// Show the dialog. This is important to do before accessing the buttons.
dialog.Show();

// Get the buttons.
var yesBtn = dialog.GetButton((int)DialogButtonType.Positive);
var noBtn = dialog.GetButton((int)DialogButtonType.Negative);

// Assign our handlers.
yesBtn.Click += (sender, args) =>
{
    // Don't dismiss dialog.
    Console.WriteLine("I am here to stay!");
};
noBtn.Click += (sender, args) =>
{
    // Dismiss dialog.
    Console.WriteLine("I will dismiss now!");
    dialog.Dismiss();
};

这篇关于Xamarin C# - Android的 - 从收盘PositiveButton点击防止AlertDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆