无法使用Gtk Builder加载glade文件。 Gtk3#

时间:2015-10-17 20:24:56

标签: c# mono gtk# glade

(注意:我没有编写此代码,它是从插件项目模板生成的)

当我尝试使用带有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格式来修复它。

1 个答案:

答案 0 :(得分:1)

我在外部修改glade文件时遇到了同样的错误。这是因为我使用的是一个我归零的缓冲区,并没有正确设置实际的文件大小。我并不认为这很重要,但设置eof正确地修正了错误。不确定它是否与你的相同(多年以后这不重要)但也许是因为类似的东西。似乎是gtk的glade阅读器中的一个错误不是很强大。