在我的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