我正在尝试填充一个Libgee HashMap,其中每个条目都有一个字符串作为键,一个函数作为值。这个是可能的吗?我想要这样的东西:
var keybindings = new Gee.HashMap<string, function> ();
keybindings.set ("<control>h", this.show_help ());
keybindings.set ("<control>q", this.explode ());所以我最终可以做这样的事情:
foreach (var entry in keybindings.entries) {
uint key_code;
Gdk.ModifierType accelerator_mods;
Gtk.accelerator_parse((string) entry.key, out key_code, out accelerator_mods);
accel_group.connect(key_code, accelerator_mods, Gtk.AccelFlags.VISIBLE, entry.value);
}但也许这不是最好的方式?
发布于 2011-05-27 07:24:23
委派就是你要找的。但据我所知,泛型不支持委托,所以一种不太优雅的方式是包装它:
delegate void DelegateType();
private class DelegateWrapper {
public DelegateType d;
public DelegateWrapper(DelegateType d) {
this.d = d;
}
}
Gee.HashMap keybindings = new Gee.HashMap<string, DelegateWrapper> ();
keybindings.set ("<control>h", new DelegateWrapper(this.show_help));
keybindings.set ("<control>q", new DelegateWrapper(this.explode));
//then connect like you normally would do:
accel_group.connect(entry.value.d);发布于 2011-05-29 03:24:04
只有具有CCode (has_target = false)的代理才有可能,否则您必须按照takoi的建议创建一个包装器。
https://stackoverflow.com/questions/6145635
复制相似问题