尝试将新菜单项添加到 NW.js(Node WebKit)中的子菜单。
if (this.menu.createMacBuiltin) {
this.menu.createMacBuiltin('Menu');
this.menuItem = this.menu.items[0];
isMac=true;
} else {
this.menuItem = new gui.MenuItem({label: 'Menu'});
this.menuItem.submenu = new gui.Menu();
this.menu.append(this.menuItem);
}
this.menuItemSubmenu = this.menuItem.submenu;
但是,像这样动态添加一个menuItem
this.newMenuItem = new gui.MenuItem({label:'New'});
this.menuItmeSubmenu.insert(this.newMenuItem,0);
不适用于 Windows,但适用于 Mac。当我重新启动 Windows 应用程序时,菜单项确实出现了。
为什么 Windows 不自动更新菜单?我该如何解决?
最佳答案
很难判断,因为您只发布了一小部分代码,并且没有提及您是否在配置文件中使用了 node-main
。
这可能是由于竞争条件引起的——也许第一个代码在第二个之后运行。尝试在进行动态插入之前打印出 this.newMenuItem.items
。你看到现有的托盘菜单了吗?
尝试将 insert
更改为 append
- 托盘菜单项出现了吗?
如果两者都正常,请尝试此解决方法:不添加新项目,而是重建列表。首先,使用 :
清空它for (var i = 0; i < this.newMenuItem.items.length; i++){
this.newMenuItem.removeAt(0);
}
然后使用append
重新添加所有项目。成功了吗?
更新
我问你是否使用 node-main
的原因是当你使用它时,在你设置为 node-main
的主脚本上,而这个脚本正在运行 window
尚未定义:
window: defined as a property of 'global', points to the DOM window global object. Note that it would be updated upon page navigation. This symbol is not available at the time the script is loaded, because the script is executed before the DOM window load (source)
为了克服这个问题,您需要在 main
脚本下运行任何需要 window
对象的代码,如 package.json
文件中所配置.
关于javascript - 在 nw.js 中添加菜单项不会显示在 Windows 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32259212/