编程语言
首页 > 编程语言> > 开始使用Mono,C#和Glade#:如何制作窗口?

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

作者:互联网

我一直试图开始使用Mono& amp; GTK#(我来自Qt / C GUI编程的背景)并决定从一个非常简单的测试GUI开始.

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

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

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

我也尝试用the GtkSharpBeginnersGuide on the mono website上的那个替换Glade GUI xml(并在C#代码中将wndTestWindow更改为window1),这似乎完全暗示我的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>

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

版本:

Windows:XP Service Pack 3
格莱德:3.6.7
MCS版本2.6.7.0
单声道和使用Mono网站上的mono-2.6.7-gtksharp-2.12.10-win32-2.exe安装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中,可以在窗口的Common properties选项卡下找到该属性.

标签:c,mono,glade
来源: https://codeday.me/bug/20190526/1158173.html