1 回答

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超5個贊
事實(shí)證明,解決方案非常簡單。我所要做的就是繼承
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);
}
}
當(dāng)然,為了使這項(xiàng)工作正常進(jìn)行,我們的主窗口應(yīng)該具有新類的類型。
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 關(guān)注
- 122 瀏覽
添加回答
舉報