c# - Mono、C# 和 Glade 入门# : How to make window appear?

标签 c# mono glade

我一直在尝试开始使用 Mono 和 GTK#(我有 Qt/C++ GUI 编程背景)并决定从一个非常简单的测试 GUI 开始。

我安装了 MS Windows Mono/GTK# 安装程序,然后发现指向 Glade 的开始菜单链接不起作用(因为它似乎没有包含在包中),我使用了“Glade with GTK+”来自 Glade 网站的 Windows 二进制安装程序。

然后我在 Glade 中创建了一个非常简单的 GUI(在本文底部)并编写了我的第一段 C# 代码来显示它。但是,它似乎无法正常工作。

“你好,世界!”正确地打印到控制台,然后程序挂起,没有出现窗口或打印任何错误消息。看起来好像窗口已经创建并且事件循环已经开始但是它没有变得可见。我已经尝试从 new Glade.XML 行中删除第一个 null 并且不将 glade 文件作为资源包括在内,但这没有任何区别。

我还尝试用 the GtkSharpBeginnersGuide on the mono website 上的那个替换 Glade GUI xml (并在 C# 代码中将 wndTestWindow 更改为 window1)并且它似乎可以完美运行,这意味着我的 Glade XML 存在问题。但是,我发现很难找出我的 Glade 安装创建的 XML 有什么问题。任何人都可以提供任何建议吗?

空 map 形用户界面:

<?xml version="1.0"?>
<glade-interface>
  <!-- interface-requires gtk+ 2.12 -->
  <!-- interface-naming-policy project-wide -->
  <widget class="GtkWindow" id="wndTestWindow">
    <property name="title" translatable="yes">Test Window</property>
    <property name="window_position">center</property>
    <child>
      <widget class="GtkVBox" id="vboxTopLevel">
        <property name="visible">True</property>
        <property name="orientation">vertical</property>
        <child>
          <widget class="GtkHBox" id="hboxComboList">
            <property name="visible">True</property>
            <child>
              <widget class="GtkLabel" id="lblList">
                <property name="visible">True</property>
                <property name="label" translatable="yes">Here's a list:</property>
              </widget>
              <packing>
                <property name="position">0</property>
              </packing>
            </child>
            <child>
              <widget class="GtkComboBox" id="cmbList">
                <property name="visible">True</property>
              </widget>
              <packing>
                <property name="position">1</property>
              </packing>
            </child>
            <child>
              <widget class="GtkButton" id="btnList">
                <property name="label" translatable="yes">Do something</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
              </widget>
              <packing>
                <property name="position">2</property>
              </packing>
            </child>
          </widget>
          <packing>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <widget class="GtkHButtonBox" id="hbtnboxButtonRow">
            <property name="visible">True</property>
            <child>
              <widget class="GtkButton" id="btnOK">
                <property name="label">gtk-ok</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <property name="use_stock">True</property>
              </widget>
              <packing>
                <property name="expand">False</property>
                <property name="fill">False</property>
                <property name="position">0</property>
              </packing>
            </child>
            <child>
              <widget class="GtkButton" id="btnCancel">
                <property name="label">gtk-cancel</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <property name="use_stock">True</property>
              </widget>
              <packing>
                <property name="expand">False</property>
                <property name="fill">False</property>
                <property name="position">1</property>
              </packing>
            </child>
          </widget>
          <packing>
            <property name="position">1</property>
          </packing>
        </child>
      </widget>
    </child>
  </widget>
</glade-interface>

C# 测试代码:

using Glade;
using Gtk;
using System;

class TestApplication
{
    static void Main(string[] args)
    {
        System.Console.Write("Hello, World!\n");
        new TestApplication(args);
    }

    public TestApplication(string[] args)
    {
        Gtk.Application.Init();

        Glade.XML gxml = new Glade.XML(null, "test_mono.glade", "wndTestWindow", null);
        gxml.Autoconnect(this);
        Gtk.Application.Run();
    }
}

编译并运行:

mcs -pkg:glade-sharp-2.0 -resource:test_mono.glade test_mono.cs
mono .\test_mono.exe

版本:

window :XP Service Pack 3 林间空地:3.6.7 MCS 版本 2.6.7.0 使用来自 Mono 网站的 mono-2.6.7-gtksharp-2.12.10-win32-2.exe 安装 Mono 和 GTK#。

使用 cygwin 和“Mono-2.6.7 命令提示符”编译和测试。

最佳答案

尝试添加 <property name="visible">True</property>到您的根小部件,以便它读取:

<?xml version="1.0"?>
<glade-interface>
  <!-- interface-requires gtk+ 2.12 -->
  <!-- interface-naming-policy project-wide -->
  <widget class="GtkWindow" id="wndTestWindow">
    <property name="visible">True</property>
    <property name="title" translatable="yes">Test Window</property>
    <property name="window_position">center</property>
    <child>

在 Glade 中,可以在窗口的“通用属性”选项卡下找到该属性。

关于c# - Mono、C# 和 Glade 入门# : How to make window appear?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3608157/

相关文章:

python - 无法在林间空地生成 GtkBuilder.xml

c# - 使用 WPF 在磁盘上存储键/值对

c# - 以下代码/设计是否存在并发问题

c# - 在自定义数据对象中使用数组 (WPF C#)

c# - 绑定(bind)到 DependencyProperty 仅适用于 "MyValue",不适用于 "{Binding PropertyHoldingMyValue}"

c# - 用于 Linux/Unix 的替代 HttpClientHandler

c# - 单声道反序列化问题

c - Gtk+3 &C & Glade 问题

python - 如何在ubuntu的右键菜单中添加一个选项

c# - Linux Mono + Postsharp + Log4Net = 没有自动异常捕获