【发布时间】: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 格式来修复它。
【问题讨论】: