Gtk#销毁事件

3
在Gtk#中,我可以为“destroy”事件挂钩处理程序,但它从未被调用。该应用程序从未输出任何内容。Gtk文档说,“destroy”事件仅在设置了某个标志时发送,并且还说该标志由Gdk自动设置。
这很令人沮丧,因为它基本上与正常的Gtk+代码相同,而且能够正确工作。除非监听“destroy”事件,否则如何知道何时调用Application.Quit()
using System;
using Gtk;

public class MainWindow {
    public static void Main(string[] args)
    {
        Application.Init();
        Window win = new Window("Test");
        win.Resize(200, 200);
        win.DestroyEvent += new DestroyEventHandler(OnDestroy);
        win.ShowAll();
        Application.Run();
    }

    private static void OnDestroy(object o, DestroyEventArgs args)
    {
        Console.WriteLine("OnDestroy");
    }
}

顺便说一句,我对“删除”事件不感兴趣。


为什么你对删除事件不感兴趣? - ptomato
@ptomato:Gtk文档:“如果用户请求关闭顶层窗口,则发出delete-event信号”--我想知道窗口何时关闭,而不是用户请求关闭的时间。 - Dietrich Epp
1个回答

4
gtk-sharp包装器安装了自己的销毁事件处理程序,该处理程序首先被调用并断开您的处理程序。但是,它提供了一个Destroyed事件。您可以按照以下方式使用它:
using System;
using Gtk;

public class MainWindow {
    public static void Main(string[] args)
    {
        Application.Init();
        Window win = new Window("Test");
        win.Resize(200, 200);
        win.Destroyed += new EventHandler(OnDestroy);
        win.ShowAll();
        Application.Run();
    }

    private static void OnDestroy(object o, EventArgs args)
    {
        Console.WriteLine("OnDestroy");
        Application.Quit();
    }
}

好的,这个可以工作,但现在的问题是它在哪里被记录了。我在文档中没有看到“Destroyed”事件: http://docs.go-mono.com/?link=T%3aGtk.Widget%2fE - Dietrich Epp
记录?这是开源的!LOL。说真的,我需要在源代码中深入挖掘才能找出你的代码为什么不起作用。文档确实应该指出这一点,虽然它确实有Destroyed事件(它是从Gtk.Object继承来的)。 - Jester

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接