【问题标题】:Cannot load glade file with Gtk Builder. Gtk3#无法使用 Gtk Builder 加载 glade 文件。 gtk3#
【发布时间】:2015-10-17 20:24:55
【问题描述】:

(注意:这段代码不是我写的,它是从插件项目模板生成的)

当我尝试使用带有 Gtk# 的 GTK Builder 来加载 glade 文件时,它会引发文件必须以元素开头的异常。

错误:

GLib.GException has been thrown
Error on line 1 char 1: Document must begin with an element 
(e.g. <book>)

MainWindow.glade:

<?xml version="1.0" encoding="UTF-8"?>
<interface>
    <requires lib="gtk+" version="3.0" />
    <object class="GtkWindow" id="window1">
        <property name="can_focus">False</property>
        <child>
            <object class="GtkBox" id="box1">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="orientation">vertical</property>
                <child>
                    <object class="GtkLabel" id="label1">
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="yalign">0.47999998927116394</property>
                        <property name="label" translatable="yes">Hello World! This button has been clicked 0 time(s).</property>
                    </object>
                    <packing>
                        <property name="expand">False</property>
                        <property name="fill">True</property>
                        <property name="position">0</property>
                    </packing>
                </child>
                <child>
                    <object class="GtkButton" id="button1">
                        <property name="label" translatable="yes">Click me!</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="receives_default">True</property>
                    </object>
                    <packing>
                        <property name="expand">False</property>
                        <property name="fill">True</property>
                        <property name="position">1</property>
                    </packing>
                </child>
            </object>
        </child>
    </object>
</interface>

MainWindow.cs:

using System;
using Gtk;
using UI = Gtk.Builder.ObjectAttribute;

public partial class MainWindow: Gtk.Window
{
    Builder builder;

    [UI] Gtk.Button button1;
    [UI] Gtk.Label label1;

    int clickedTimes;

    public static MainWindow Create ()
    {
        Builder builder = new Builder (null, "DiscordEditor.interfaces.MainWindow.glade", null);
        return new MainWindow (builder, builder.GetObject ("window1").Handle);
    }

    protected MainWindow (Builder builder, IntPtr handle) : base (handle)
    {
        this.builder = builder;

        builder.Autoconnect (this);
        DeleteEvent += OnDeleteEvent;

        button1.Clicked += onButtonClick;
    }

    protected void onButtonClick (object sender, EventArgs a)
    {
        clickedTimes++;
        label1.Text = string.Format ("Hello World! This button has been clicked {0} time(s).", clickedTimes);
    }

    protected void OnDeleteEvent (object sender, DeleteEventArgs a)
    {
        Application.Quit ();
        a.RetVal = true;
    }
}

我不明白为什么会发生这种情况,除非它是 GTK#3 的错误,因为它还没有完全发布。我真的很想使用 GTK3 而不是 GTK2,那么有人会建议什么?

编辑:我通过打开生成的文件并将其保存为 GTKBuilder 格式来修复它。

【问题讨论】:

    标签: c# mono gtk# glade


    【解决方案1】:

    我在外部修改 glade 文件时遇到了同样的错误。这是因为我使用了一个归零的缓冲区并且没有正确设置实际文件大小。我认为这并不重要,但正确设置 eof 可以修复错误。不确定它是否与您的相同(多年后并不重要),但可能是由于类似的原因。似乎是 gtk 的 glade reader 不是很健壮的错误。

    【讨论】:

    • 您能否详细说明“正确设置 eof”是什么意思?当您说“缓冲区”时,您是指 GtkTextBuffer 吗?
    • @grandchild:当创建和过度分配内存缓冲区而不是 GtkTextBuffer 并将其清零时,将缓冲区的每个元素设置为 0。现在,如果一个人将文本写入缓冲区以创建一个glade ui 文件并写入它,文件可能以 0 结尾,glade ui 文件阅读器然后错误地处理它。我想发生了什么是解析器遇到空字符并崩溃了。
    • 好的,谢谢。我最终做的是将 xml 文件的 contents 复制到具有 不同名称 的文件(使用文本编辑器),然后神奇地修复了它。这似乎与您所指的缓冲区问题有关,但更奇怪。因为现在 gui_new.glade 工作了,但是如果我删除了原来的 gui.glade 并将新创建的 gui_new.glade 文件重命名回 gui.glade 它将再次停止工作。也许与文件句柄或 Glade 备份文件有关……真的不确定。但是使用新的文件名是可行的,所以我正在这样做。
    【解决方案2】:

    为我解决了类似问题的方法是将 .glade 文件的内容复制到具有新名称的文件中,然后在代码中加载该文件。我无法解释为什么这会起作用,但它确实起作用了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-24
      • 1970-01-01
      • 2020-02-11
      • 1970-01-01
      相关资源
      最近更新 更多