Xamarin、C#、ALertDialog 和 OnBackPressed

Xamarin, C#, ALertDialog and OnBackPressed

我有以下代码:

using Android.App;
using Android.OS;
using Android.Util;
using Android.Views;

namespace TestIt {
    [Activity( Label = "TestIt", MainLauncher = true, Icon = "@drawable/icon", Theme = "@android:style/Theme.Dialog" )]
    public class TestIt:Activity {

        public static readonly string progName = "TestIt";

        public static AlertDialog builder = null;

        protected override void OnCreate(Bundle bundle) {

            base.OnCreate(bundle);

            Log.Debug(progName, "OnCreate entered");

            builder = (new AlertDialog.Builder( this )).Create();

            Log.Debug(progName, "Build Alert");

            builder.Window.SetType(WindowManagerTypes.SystemAlert);
            builder.SetCancelable(true);
            builder.SetTitle("Test");
            builder.SetMessage("This is a test message");
            builder.Show();

            Log.Debug(progName, "Build Alert Ending"); 

        }

        public override void OnBackPressed() {
            Log.Debug(progName, "OnBackPressed Entered");

            if(builder != null) {
                builder.Cancel();
            }

            base.OnBackPressed();
            Finish();
        }
    }
}

一切正常并显示警报。

但是当按下返回键时,不会进入 OnBackPressed。

相反,我在 LogCat 中收到消息:

Attempted to finish an input event but the input event receiver has already been disposed.

我已经看到几个 Java 和几个 Xamarin 尝试解决这个问题,但该技术通常深埋在示例的功能中。

有人可以提供一些关于如何调整此代码以便输入 OnBackPressed(或替代方法)的 C# (Xamarin) 见解。

我只需要到达返回键即可。

此致, 吉姆

这是因为,对话框首先消耗了后退按钮的按下。此按取消对话框。再按一次后退按钮将调用您的重载方法。

假设 如果用户取消对话框,您想要关闭 activity。如果是这样,请对此做出反应:

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);

    // ...

    // attach the event listener
    builder.CancelEvent += OnDialogCancel;
    builder.Show();
}

private void OnDialogCancel(object sender, EventArgs eventArgs)
{
    builder.CancelEvent -= OnDialogCancel;
    Finish();
}

如果您确实需要在显示对话框时按下后退按钮,则必须继承您自己的对话框并在那里覆盖 OnBackPressed

public class MainActivity : Activity
{
    // ...
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // ...
        builder = new MyAlertDialog(this);

        Log.Debug(progName, "Build Alert");
        builder.SetCancelable(true);
        builder.SetTitle("Test");
        builder.SetMessage("This is a test message");
        builder.Show();

        Log.Debug(progName, "Build Alert Ending");
    }
}

public class MyAlertDialog : AlertDialog
{
    public MyAlertDialog(Context context) : base(context)
    {
    }

    public override void OnBackPressed()
    {
        Log.Debug("foo", "OnBackPressed Entered");
        base.OnBackPressed();
    }
}
public override void OnBackPressed()
    {
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.SetTitle("msgConfirm");
        alert.SetMessage("msgSureExit");
        alert.SetPositiveButton("msgYes"), (sender, args) =>
        {
            this.FinishAffinity();
        });
        alert.SetNegativeButton("msgNo"), (sender, args) =>
        {

        });
        Dialog dialog = alert.Create();
        dialog.Show();
    }

This is more simpler way.hope this helps someone.If this is useful then reply @user1047857. :)happy coding.