2013-03-24 26 views

回答

0

那麼,我不知道builder對象是否被實現,因爲我也是GTK#的新手,但Autoconnect()方法的確適用於Glade.XML對象,這就是我如何連接我的空地中的信號XML。下面是使用自動連接的信號,一個簡單的HelloWorld C#程序的工作示例:

(我有GTK#20年2月12日,和沼地3.4.3)使用系統

;使用Gtk的 ;使用Glade的 ;

命名空間textPad { 公共類GladeApp { 公共靜態無效的主要(字符串[]參數) { 新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(); 
    } 
} 
} 
+0

是這個工程的空地,但不GtkBuilder :( – Petr 2013-03-24 21:28:43

+0

)如果你使用glade而不是gtk-builder,它會有什麼不同?最終,你的GUI應用程序會運行,這是什麼問題。使用gtk-builder代替在C#中的Glade.XML? – 2013-03-25 03:11:08

+0

區別在於最新的glade甚至不支持這種過時的格式。所有這些新工具都生成GtkBuilder格式,所以我不能使用它們。 (生成的XML文件不兼容) – Petr 2013-03-25 13:13:54

3

是的,這是可以使用GTK生成器在C#中,而無需使用格萊德庫。 (順便說一下,在Boo我也取得了這樣的成功; Cobra沒有取得成功,還沒有嘗試[Iron] Python或[Iron] Ruby)。

想要使用GTK的一個非常強烈的原因而不是Glade庫的是,AFAIK,3.8之後的Glade版本產生僅與GTK + 3(http://blogs.gnome.org/tvb/2011/01/15/the-glade-dl/)兼容的XML代碼。另外,我認爲使用GTK Builder可以讓用戶使用幾乎任何生成適當定義的GUI生成器生成的XML文件。

好的,下面是適用於C#的MonoBasic示例的解決方案: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(); 
    } 
} 

下面是由林地,togglebuttons.xml生成的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 :-)

+0

這怎麼可能? XML文件要求gtk + 3.0,而Gtk#2.12包裝gtk + 2 ...我自己嘗試過,並因此引發異常。當我操作這個文件時,似乎GtkBuilder#所預期的XML格式與Glade 3.20生成的格式不同......也許以前可能(雖然我看不出來)如何,但現在是不可能的。 – Baltasarq 2017-01-19 14:14:38

+0

這不是不可能的。而且,你已經明確地說明了你爲什麼會遇到問題,「XML文件要求gtk + 3.0,而Gtk#2.12包裝gtk + 2。」你需要使用「-pkg:」開關來編譯Gtk#3而不是Gtk#2。 – 2017-01-20 19:24:35

+0

好的,我很抱歉,我不知道Gtk#3作爲NuGet包。奇怪的electrion,讓我覺得它沒有完成...是嗎?好吧,我試着在Windows上安裝MSYS2,然後用pacman與gtk + 3,但沒有運氣(我同時使用Windows和Linux)。可能會在Linux中沒有問題的情況下工作。也許我必須將Xamarin Studio以某種方式指向圖書館?還沒有找到如何... – Baltasarq 2017-01-22 13:36:23