Vala GtkButton.Clicked.Connect 没有调用函数

Vala GtkButton.Clicked.Connect not calling function

我仍在努力学习 vala,但我在使用 GtkButton 信号时遇到了问题。

我想将函数 void refresh () 连接到 GtkButton。单击按钮时,应调用该函数并设置 GtkLabel 的标签。所以我写GtkButton.clicked.connect (this.function);。这应该是我点击按钮时调用的函数吧?

我的函数对于测试来说非常简单,应该更改 GtkLabel 的文本。所以我得到 void function () { GtkLabel.label = "New Text"; }.

当我测试这个小程序时,点击按钮没有任何反应,或者至少我看不到任何东西。

我错过了什么?

这是我的代码:

namespace Zeiterfassunggtk {
    [GtkTemplate (ui = "/org/gnome/Zeiterfassunggtk/window.ui")]
    public class Window : Gtk.ApplicationWindow {
        [GtkChild]
        Gtk.Button refreshbutton;
        Gtk.Button menubuttonrefresh;

        void refresh () {
            label1.label = "Clicked";
        }

        public Window (Gtk.Application app) {
            Object (application: app);

            refreshbutton.clicked.connect (this.refresh);
            menubuttonrefresh.clicked.connect (this.refresh);

            this.show_all ();
        }
    }
}

您可以在 github.com

查看完整代码

如果模板中的每个字段都需要 [GtkChild]。现在,menurefresh 包含 null 并且不会连接到任何东西。 label1 也是 null,因此更改其标签不会有任何作用。

正确的代码是:

namespace Zeiterfassunggtk {
    [GtkTemplate (ui = "/org/gnome/Zeiterfassunggtk/window.ui")]
    public class Window : Gtk.ApplicationWindow {
        [GtkChild]
        Gtk.Button refreshbutton;
        [GtkChild]
        Gtk.Button menubuttonrefresh;
        [GtkChild]
        Gtk.Label label1;

        void refresh () {
            label1.label = "Clicked";
        }

        public Window (Gtk.Application app) {
            Object (application: app);

            refreshbutton.clicked.connect (this.refresh);
            menubuttonrefresh.clicked.connect (this.refresh);

            this.show_all ();
        }
    }
}