【发布时间】:2016-12-06 21:41:50
【问题描述】:
我现在正在努力学习 vala。在我的示例应用程序中,GLib.Menu 操作有问题。
我声明了一个应该退出应用程序的新操作 quit_action。编译器运行时没有任何警告或错误,但是当我运行应用程序时,我可以打开菜单,但“退出”项是灰色的。
/* main.vala */
using Gtk;
class mainWindow : Gtk.ApplicationWindow {
public mainWindow (Application app) {
// Window Properties
this.set_default_size (800, 600);
this.window_position = Gtk.WindowPosition.CENTER;
this.destroy.connect (Gtk.main_quit);
// HeaderBar
var headerBar = new Gtk.HeaderBar ();
headerBar.set_title ("Test Title");
headerBar.set_subtitle ("Test Subtitle");
headerBar.set_show_close_button (true);
// Menu
var menuButton = new Gtk.Button.from_icon_name ("open-menu-symbolic", Gtk.IconSize.SMALL_TOOLBAR);
headerBar.pack_end (menuButton);
var menu = new GLib.Menu ();
menu.append ("Quit", "app.quit");
var popover = new Gtk.Popover (menuButton);
popover.bind_model (menu, "app");
menuButton.clicked.connect (() => {
popover.popup ();
popover.show_all ();
});
this.set_titlebar (headerBar);
this.show_all ();
}
}
public class Application : Gtk.Application {
public Application () {
Object (application_id: "org.example.application", flags: 0);
}
protected override void activate () {
// Create a new window for this application.
mainWindow win = new mainWindow (this);
win.show_all ();
Gtk.main ();
}
protected override void startup () {
base.startup ();
var quit_action = new GLib.SimpleAction ("quit", null);
quit_action.activate.connect (this.quit);
this.add_action (quit_action);
}
}
int main (string[] args) {
Gtk.init (ref args);
return new Application ().run (args);
}
【问题讨论】: