(Note: I did not write this code, it was created from the addon project template)
When I try to use GTK Builder with Gtk # to load a glade file, it throws an exception that should start with the element.
Mistake:
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;
}
}
I do not understand why this will happen, unless it is a bug with GTK # 3, as it is not yet fully released. I really would like to use GTK3 instead of GTK2, so what would anyone suggest?
Edit: I fixed it by opening the generated file and saving it in GTKBuilder format.
source
share