我如何在c#中使用gtk builder

时间:2013-03-24 14:12:37

标签: c# gtk#

根据How do I connect glade signals using GtkBuilder in C#? 2009年,mono的开发人员即将实现gtk builder到c#。它是2013年,提出的方法仍然没有。那么有什么方法可以使用它吗?

由于GtkBuilder取代了glade格式,整个glade对c#似乎没什么用处(至少glade编辑器将文件保存为GtkBuilder格式,libglade单声道无法读取)

2 个答案:

答案 0 :(得分:3)

是的,可以在C#中使用GTK Builder而无需使用Glade库。 (顺便说一句,我在Boo中也取得了成功;没有在Cobra上取得成功,也没有尝试[Iron] Python或[Iron] Ruby。)

想要使用GTK构建器而不是Glade库的一个非常强大的理由是,AFAIK,Glade版本3.8之后产生的XML代码仅与GTK + 3(http://blogs.gnome.org/tvb/2011/01/15/the-glade-dl/)兼容。另外,我认为使用GTK Builder可以使用几乎任何生成适当定义的GUI构建器生成的XML文件。

好的,所以这里的解决方案适用于MonoBasic示例中的C#:http://www.mono-project.com/GtkSharp:_Hello_World。我使用该示例中的GUI定义在Glade 3.14.2中创建GUI,然后将文件保存为“togglebuttons.xml”。

togglebuttons.cs:

using Gtk;
using System;

class ToggleButtons
{
    public ToggleButtons()
    {
        Gtk.Application.Init();
        Builder Gui = new Builder();
        Gui.AddFromFile("togglebuttons.xml");
        Gui.Autoconnect(this);
        Gtk.Application.Run();
    }

    static void onDeleteEvent(object o, DeleteEventArgs args)
    {
        Application.Quit();
    }

    static void onExitButtonEvent(object o, EventArgs args)
    {
        Application.Quit();
    }

    public static void Main()
    {
        new ToggleButtons();
    }
}

这是Glade生成的XML文件,togglebuttons.xml:

<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtk+ 3.0 -->
<object class="GtkWindow" id="window">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <property name="title" translatable="yes">Toggle Buttons</property>
    <signal name="delete-event" handler="onDeleteEvent" swapped="no"/>
    <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="GtkToggleButton" id="togglebutton1">
            <property name="label" translatable="yes">Button 1</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <property name="halign">center</property>
        </object>
        <packing>
            <property name="expand">True</property>
            <property name="fill">False</property>
            <property name="position">0</property>
        </packing>
        </child>
        <child>
        <object class="GtkToggleButton" id="togglebutton2">
            <property name="label" translatable="yes">Button 2</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <property name="halign">center</property>
        </object>
        <packing>
            <property name="expand">True</property>
            <property name="fill">False</property>
            <property name="position">1</property>
        </packing>
        </child>
        <child>
        <object class="GtkSeparator" id="separator1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
        </object>
        <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">2</property>
        </packing>
        </child>
        <child>
        <object class="GtkButton" id="button1">
            <property name="label" translatable="yes">Close</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <property name="halign">center</property>
            <signal name="clicked" handler="onExitButtonEvent" swapped="no"/>
        </object>
        <packing>
            <property name="expand">True</property>
            <property name="fill">False</property>
            <property name="position">3</property>
        </packing>
        </child>
    </object>
    </child>
</object>
</interface>

HTH: - )

答案 1 :(得分:0)

好吧,我不知道构建器对象是否已实现,因为我也是GTK#的新手,但Autoconnect()方法确实适用于Glade.XML对象,这就是我连接信号的方式在我的glade xml中。下面是一个简单的helloworld c#程序的工作示例,该程序使用Autoconnect作为信号:

(我有GTK#2.12.20和Glade 3.4.3)

使用System; 使用Gtk; 使用Glade;

命名空间textPad {     公共类GladeApp     {         public static void Main(string [] args)         {             新的GladeApp();         }

    public GladeApp(){
        //System.Console.WriteLine ("Hello GTK");
        //System.Console.Read ();
        Gtk.Application.Init ();

        Glade.XML gxml = new XML (null,@"textPad.FirstTextpad.glade","window1",null);

        gxml.Autoconnect (this);


        Gtk.Application.Run ();
        //return 0;
    }

    public void btnExit_clicked_cb(System.Object sender,System.EventArgs e)
    {
        close (null,null);
    }

    public void close(System.Object sender, System.EventArgs e)
    {
        Application.Quit ();
    }
}
}