Gee HashMap 包含方法作为值
我正在尝试填充 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);
}
但这也许不是最好的方法?
I'm trying to fill a Libgee HashMap where each entry has a string as key, and a function as value. Is this possible? I want this sort of thing:
var keybindings = new Gee.HashMap<string, function> ();
keybindings.set ("<control>h", this.show_help ());
keybindings.set ("<control>q", this.explode ());
so that I can eventually do something like this:
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);
}
But perhaps this isn't the best way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
代表就是您正在寻找的人。但上次我检查时,泛型不支持委托,所以一个不太优雅的方法是包装它:
Delegates are what you're looking for. But last time I checked, generics didn't support delegates, so a not-so-elegant way is to wrap it:
仅适用于具有 [CCode (has_target = false)] 的委托,否则您必须按照 takoi 建议创建一个包装器。
It is possible only for delegates with [CCode (has_target = false)], otherwise you have to create a wrapper as takoi suggested.