问题
In python, I could just do builder.connect_signals(self)
. It doesn't seem like this method exists in C#, and after looking at the GtkBuilder documentation, it looks like python is the exception, rather than the rule. How would I accomplish the same thing in C#?
回答1:
Right now Gtk.Builder is not fully implemented in the current version of Gtk# (2.12). This thread explains the current situation. So once Gtk# 2.14 is released, you can just do:
builder.Autoconnect (this);
In the meantime you could use Glade.XML, and then convert your code (and glade files) as described here: http://lists.ximian.com/pipermail/gtk-sharp-list/2008-October/009157.html
回答2:
You can connect your signals using method Autoconnect
, but method that represent a signal in c# must be in form:
static void customMethod(object sender, EventArgs args);
So each field of class that you use in such method must be declared as static
. It robs you of creating another instance of your class.
There is another way of connecting signals:
Builder builder = new Builder();
builder.AddFromFile("custom.glade");
Button button = (Button)builder.GetObject("closeButton");
button.Clicked += delegate {
Application.Quit();
}
来源:https://stackoverflow.com/questions/1693454/how-do-i-connect-glade-signals-using-gtkbuilder-in-c