Vala 未连接到样式 Sheet

Vala Not Connecting to Style Sheet

在使用 Java 编程几年后,我目前正在学习 Vala。

在网上进行了一些广泛的搜索后,我发现了一种使用 css 样式 sheet 设置 GTK Window 背景样式的方法,来自给定的示例 ,它使用 GTk.Window class 扩展名。代码在我的机器 (Ubuntu 19.04) 上编译得很好,小部件的样式符合预期。

我试图将这种方法与 site 中的一种方法结合起来。在这里,vala class 扩展到 Gtk.Application,而不是 Gtk.Window。

代码编译并打开 window,但小部件未根据样式 sheet 设置样式。

public class StyleApp1 : Gtk.Application {

    public StyleApp1 () {
        Object (
            application_id: "com.css.test",
            flags: ApplicationFlags.FLAGS_NONE
        );
    }

    protected override void activate () {

        var window = new Gtk.ApplicationWindow (this);
        window.set_default_size (350, 500);
        window.title = "Hello World";
        window.get_style_context().add_class("my_window");

        var screen = window.get_screen ();

        var css_provider = new Gtk.CssProvider();
        string path = "styleapp1.css";

        // test if the css file exist
        if (FileUtils.test (path, FileTest.EXISTS))
        {
            try {
                stdout.printf("File is there");
                css_provider.load_from_path(path);
                Gtk.StyleContext.add_provider_for_screen(screen, css_provider, Gtk.STYLE_PROVIDER_PRIORITY_USER);
            } catch (Error e) {
                error ("Cannot load CSS stylesheet: %s", e.message);
            }
        }

        var box = new Gtk.Box (Gtk.Orientation.VERTICAL, 10);
        window.add (box);

        var label = new Gtk.Label ("Thank you");
        box.add (label);

        var label2 = new Gtk.Label ("Whosebug");
        label2.get_style_context().add_class("my_class");
        box.add (label2);

        window.show_all ();
    }

    public static int main (string[] args) {
        var app = new StyleApp1 ();
        return app.run (args);
    }
}

CSS 文件 (syleapp1.css)

GtkWindow {
    font-size: 17px;
}

.my_class {
    color: red;
}

.my_window {
    background-color: rgba (200, 100, 100, 0.9);
 }

介子构建文件:

project('com.css.test' , 'vala' , 'c')

executable (
    meson.project_name(),
   'StyleApp1.vala',

    dependencies: [
        dependency('gtk+-3.0')
    ],
install: true
)

我不知道我错过了什么。有人可以解释并指出我正确的方向吗?

非常感谢。

在这里工作正常:

我目前正在使用 Windows / msys2。