1 回答
TA贡献1851条经验 获得超5个赞
事实证明,解决方案非常简单。我所要做的就是继承
Xamarin.Forms.Platform.GTK.FormsWindow
并覆盖
protected override bool OnDeleteEvent(Event evnt)
像这样
public class MyFormsWindow: FormsWindow
{
protected override bool OnDeleteEvent(Event evnt)
{
var messageDialog = new MessageDialog(Program.MainWindow, DialogFlags.Modal,
MessageType.Question, ButtonsType.YesNo,
"Do you want to exit?", String.Empty)
{
Title = "Confirmation",
};
int result = messageDialog.Run();
//the magic numbers stand for "Close" and "No" results
if (result == -4
|| result == -9
{
messageDialog.Destroy();
return true; // true means not to handle the Delete event by further handlers, as result do not close application
}
else
{
messageDialog.Destroy();
return base.OnDeleteEvent(evnt);
}
}
当然,为了使这项工作正常进行,我们的主窗口应该具有新类的类型。
public class Program
{
public static MyFormsWindow MainWindow { get; private set; }
[STAThread]
public static void Main(string[] args)
{
Gtk.Application.Init();
Forms.Init();
var app = new App();
var window = new MyFormsWindow();
window.LoadApplication(app);
window.SetApplicationTitle("MyApp");
window.Show();
MainWindow = window;
Gtk.Application.Run();
}
}
- 1 回答
- 0 关注
- 75 浏览
添加回答
举报