System.NotSupportedException:无法从本机句柄激活MyProject.CancelListener类型的实例

时间:2018-11-15 21:43:26

标签: xamarin.forms xamarin.android

在我的Xamarin Forms应用程序中,我正在使用本机Android代码作为平台。我想显示一个AlertDialog.Builder并在用户使用SetOnCancelListener轻触对话框外时捕获事件。这是我的代码:

AlertDialog.Builder adb = new AlertDialog.Builder(this);

adb.SetTitle("title");

adb.SetItems(myItems.Select(x => x.Name).ToArray(), (s, e) =>
{
    // not important code
});

var cancelled = new CancelListener();
cancelled.Cancelled += (s, e) =>
{
    // not important code
};
adb.SetCancelable(true);
adb.SetOnCancelListener(cancelled);

Dialog d = adb.Create();
d.Show();

我的CancelListener班:

public class CancelListener : Java.Lang.Object, IDialogInterfaceOnCancelListener
{
    public event EventHandler Cancelled;

    public IntPtr Handle => IntPtr.Zero;

    public CancelListener() : base()
    {

    }

    public void Dispose()
    {
        Cancelled = null;
    }

    public void OnCancel(IDialogInterface dialog)
    {
        Cancelled?.Invoke(null, EventArgs.Empty);
    }
}

当我在对话框外点击时,会收到消息:

  

System.NotSupportedException:无法激活类型的实例   来自本机句柄的MyProject.CancelListener

0 个答案:

没有答案