Vala GtkTemplate:UI 未找到资源

Vala GtkTemplate: UI Resource not found

我正在尝试使用 GtkTemplate,但它无法正常工作,这实在令人恼火。我已经定义了 .ui 文件资源,正确调用它等等

这是我的文件:

meson.build:

# project name and programming language
project('myproject', 'vala', 'c', version: '1.0.0')

# add resources to the executeable
gnome = import('gnome')
gresources = gnome.compile_resources(
    meson.project_name() + '.resources',
    'data/gresources.xml',
    c_name: 'resources'
)

executable(
    meson.project_name(),

    'src/OpenFileWindow.vala',
    'src/Main.vala',

    gresources,

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

src/OpenFileWindow.vala:

using Gtk;

namespace MyProject {

    [GtkTemplate (ui="/ui/OpenFileWindow.ui")]
    public class OpenFileWindow : Window {
        [GtkChild]
        Button btn_browse;

        public OpenFileWindow() {

        }

        [GtkCallback]
        private void btn_browse_clicked(Button btn) {
            stdout.printf("CLICKED");
        }
    }
}

ui/OpenFileWindow.ui:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <template class="OpenFileWindow" parent="GtkWindow">
    <property name="can_focus">False</property>
    <child>
      <object class="GtkButton" id="btn_browse">
        <property name="label">gtk-open</property>
        <property name="visible">True</property>
        <property name="can_focus">True</property>
        <property name="receives_default">True</property>
        <property name="use_stock">True</property>
        <property name="image_position">top</property>
        <property name="always_show_image">True</property>
        <signal name="clicked" handler="myproject_openfilewindow_btn_browse_clicked" swapped="no"/>
      </object>
    </child>
  </template>
</interface>

data/gresources.xml:

<?xml version="1.0" encoding="UTF-8"?>
<gresources>
  <gresource prefix="/">
    <file preprocess="xml-stripblanks">ui/OpenFileWindow.ui</file>
  </gresource>
</gresources>

当我ui与 meson & ninja 建立联系时,它给出了这个错误:

valac -C --debug --debug --pkg gio-2.0 --pkg gtk+-3.0 --color=always --directory myproject@exe --basedir ../ --gresources=../data/gresources.xml ../src/OpenFileWindow.vala ../src/Main.vala

../src/OpenFileWindow.vala:6.5-6.40: error: UI resource not found: `/ui/OpenFileWindow.ui'. Please make sure to specify the proper GResources xml files with --gresources and alternative search locations with --gresourcesdir.
    public class OpenFileWindow : Window {

什么问题,我真的看不出来...谢谢!

GResource 是一个只读文件系统,用于已嵌入到已编译二进制文件中的文件。在 Vala GUI 项目中,这可用于在二进制文件中存储图像、图标等。

GtkBuilder UI definition files can also be embedded and Vala has additional support for this with the [GtkTemplate], [GtkChild] and [GtkCallback] attributes. Part of this support includes type checking at compile time. The check needs to get the source file and work out the filename for the GResource in-memory file system and this is where the project is failing. Anyone wanting to improve this should patch codegen/valagtkmodule.vala 在 Vala 编译器中。但是现在,为了使您的项目正常运行,您需要为资源使用更扁平的文件结构。

首先将data/gresource.xml移动到ui/目录。然后将 prefix 更改为 /ui。这意味着内存文件系统将使用与您在 Vala 代码中使用的 GtkTemplate 属性相匹配的名称。还要从文件名中删除 ui/ 以提供平面目录结构:

<?xml version="1.0" encoding="UTF-8"?>
<gresources>
  <gresource prefix="/ui/">
    <file preprocess="xml-stripblanks">OpenFileWindow.ui</file>
  </gresource>
</gresources>

你还需要修改meson.build文件中gresources的定义:

gresources = gnome.compile_resources(
    meson.project_name() + '.resources',
    'ui/gresources.xml',
    source_dir: ['ui']
)

这使用 source_dir 来保持引用在扁平目录结构中的工作。

您现在应该不会再收到错误消息。看看 Geary 作为一个使用大量 GtkBuilder UI 文件的示例项目。该项目的 GResource 文件与其他文件位于同一目录中。

您的项目仍然无法编译,因为 Vala 编译器已经识别出 [GtkCallback] 没有信号。这是一个名称解析问题,您只需更改 OpenFileWindow.ui 文件中的一行:

<signal name="clicked" handler="myproject_openfilewindow_btn_browse_clicked" swapped="no"/>

<signal name="clicked" handler="btn_browse_clicked" swapped="no"/>

更新

对于 Vala 编译器类型检查 GtkBuilder UI 定义文件,gresource.xml 只需要在一个共同的祖先目录中。这使得像这样的资源目录结构成为可能:

resources/
├── components/
│   └── zoom-bar.ui
├── icons/
│   └── project.svg
├── layouts/
│   └── main-window.ui
├── themes/
│   ├── dark.css
│   └── light.css
└── filelist.gresource.xml

filelist.gresource.xml 文件需要 /prefix,但子目录从源文件复制到内存文件:

<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/">
<file preprocess="xml-stripblanks" compressed="true">layouts/main-window.ui</file>
<file preprocess="xml-stripblanks" compressed="true">components/zoom-bar.ui</file>
</gresource>
</gresources>

然后可以通过以下方式在 Vala 中访问:

[GtkTemplate (ui="/layouts/main-window.ui")]

meson.buildgnome.compile_resources()source_dir 参数仍需设置:

gresources = gnome.compile_resources(
    'project-resources',
    'resources/filelist.gresource.xml',
    source_dir: ['resources']
)