管理 GTK 中的信号 .ui vala 中的文件

manage signals in GTK .ui files in vala

从以下 .ui 文件中,我有信号:

     <object class="GtkButton" id="button2">
        <property name="label">Button 2</property>
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="receives_default">False</property>
        <signal name="clicked" handler="on_button2_clicked" swapped="no"/>
      </object>

信号名称 on_button2_clicked 和以下我用来处理事件的代码 add_callback_symbool :

    // Create Builder
    var build = new Gtk.Builder ();
    build.add_from_resource ("/me/rush/Rush/main.ui");

    // Create an instance
    var window =  (Gtk.ApplicationWindow) build.get_object ("window");

    // handle event
    build.add_callback_symbol ("on_button2_clicked", ()=> {print ("click");});

    window.application = app;
    window.show_all ();

GUI 出现在屏幕上,但事件处理 on_button2_clicked 不起作用,我尝试使用 build.connect_signals (null) 处理事件和具有相同名称的 defiend 函数,但也不起作用

在谷歌搜索时,我找到了一种在模板中使用 callbaks 的方法,但我想做的是使用 connect.signalsadd_callback_symbol 处理事件 我也试过示例 here 但它不起作用

我也对 your question on GNOME's discourse 回复了此问题,但我会 post 在这里再次回复以提高可见性:

It's maybe good to know that Vala has built-in support for GtkTemplates, which means you don't need to explicitly call the GtkBuilder API anymore. Vala will do the necessary checks for you (at compile-time even).

You can find multiple examples for this in the GNOME Contacts repository, but to give a concrete one, let's show the SetupWindow: