在我的Xamarin表单应用程序中,我使用的是平台的原生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:无法激活类型为的实例