开始使用Mono,C#和Glade#:如何制作窗口?

NPB*_*NPB 4 c# mono glade

我一直在努力开始使用Mono和GTK#(我来自Qt/C++ GUI编程的背景),并决定从一个非常简单的测试GUI开始.

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

然后我在Glade中创建了一个非常简单的GUI(在这篇文章的底部)并编写了我的第一个C#代码来显示它.但是,它似乎没有正常工作.

"Hello,World!" 正确打印到控制台,然后程序挂起,不显示窗口或打印任何错误消息.看起来好像已经创建了窗口并且事件循环已经启动但是它没有被显示.我已经尝试nullnew Glade.XML行中删除第一个并且不包括作为资源的glade文件,但这没有任何区别.

我也尝试用单声道网站的GtkSharpBeginnersGuide替换Glade GUI xml (并wndTestWindow改为window1使用C#代码),这似乎完全暗示我的Glade XML存在问题.但是,我发现很难弄清楚我的Glade安装创建的XML有什么问题.有人可以提供任何建议吗?

Glade GUI:

<?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>
Run Code Online (Sandbox Code Playgroud)

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

编译并运行:

mcs -pkg:glade-sharp-2.0 -resource:test_mono.glade test_mono.cs
mono .\test_mono.exe
Run Code Online (Sandbox Code Playgroud)

版本:

Windows:XP Service Pack 3 Glade:使用mono-2.6.7-gtksharp-2.12.10-win32-2.exeMono网站安装的3.6.7 MCS版本2.6.7.0 Mono和GTK#.

使用cygwin和"Mono-2.6.7命令提示符"进行编译和测试.

sto*_*oft 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>
Run Code Online (Sandbox Code Playgroud)

在Glade中,可以在窗口的Common properties选项卡下找到该属性.