gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:42:34 25 4
gpt4 key购买 nike

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

当我尝试将 GTK Builder 与 Gtk# 结合使用来加载 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>

主窗口.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 格式来修复它。

最佳答案

我在外部修改glade文件时出现了同样的错误。这是因为我使用的缓冲区已归零并且没有正确设置实际文件大小。我认为这无关紧要,但正确设置 eof 可以修复错误。不确定它在你身上是否相同(多年后这并不重要)但也许是由于类似的原因。似乎是 gtk 的 glade 阅读器不是很健壮的一个错误。

关于c# - 无法使用 Gtk Builder 加载 glade 文件。 Gtk3#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33191245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com