我有一个用 Vala 编写的非常基本的 GTK 应用程序(此处为非工作源树),它打算从 GResource 加载主应用程序窗口的资源。
所以我创建了一个资源文件,用它编译并将其glib-compile-resources添加到VALAFLAGSas --gresources=$(top_srcdir)/data/gauthenticator.gresource.xml.
数据文件的相关部分如下所示:
<gresource prefix="/eu/polonkai/gergely/gauthenticator">
<file preprocess="xml-stripblanks">gauth-window.ui</file>
</gresource>
Run Code Online (Sandbox Code Playgroud)
我像这样使用它:
[GtkTemplate (ui = "/eu/polonkai/gergely/gauthenticator/gauth-window.ui")]
class Window : Gtk.ApplicationWindow {
[GtkChild]
private Gtk.ProgressBar countdown;
}
Run Code Online (Sandbox Code Playgroud)
我的相关部分Makefile.am:
gresource_file = $(top_srcdir)/data/gauthenticator.gresource.xml
gauthenticator_VALAFLAGS = --pkg gtk+-3.0 --target-glib=2.38 --gresources $(gresource_file)
Run Code Online (Sandbox Code Playgroud)
编译期间一切正常,但在运行时出现此错误:
(gauthenticator:16501): Gtk-CRITICAL **: Unable to load resource for composite template for type 'GAuthenticatorWindow': The resource at '/eu/polonkai/gergely/gauthenticator/gauth-window.ui' does not exist
(gauthenticator:16501): Gtk-CRITICAL **: gtk_widget_class_bind_template_child_full: assertion 'widget_class->priv->template != NULL' failed
(gauthenticator:16501): Gtk-CRITICAL **: gtk_widget_init_template: assertion 'template != NULL' failed
Run Code Online (Sandbox Code Playgroud)
我从 GNOME Boxes 存储库复制了大多数与资源相关的行,但显然错过了一些东西。
你是如何编译资源的glib-compile-resources?我建议将其编译为 C 文件:
glib-compile-resources --sourcedir data --generate-source --target my_build_dir/resources/resources.c data/gauthenticator.gresource.xml
然后添加my_build_dir/resources/resources.c到您的,_SOURCES以便它与您的 Vala 源一起编译。在--gresources供选择valac只做类型检查的瓦拉的GTK +复合模板支撑。
我没有尝试将 GResource C 文件编译为 Vala 到 C 的一部分,然后是 C 到二进制编译过程。目前看起来您只是在valac用于整个编译过程。