问题描述
与任何标准原生应用程序一样,我的电子应用程序也需要根据实时使用结果更改多个菜单项的状态(启用/禁用).
Like in any standard native application, also my electron's application needs to change the status (enabled/dsabled) of several menu item, based on live usage results.
我正在 main.js 中设置我的菜单:
I am setting up my menu in main.js:
function createWindow () {
...
...
require('./menu/mainmenu');
}
我需要更改的 MenuItem 在 mainmenu 中定义:
The MenuItem I need to change is defined in mainmenu:
{ label: "Show Colors",
accelerator: 'CmdOrCtrl+1',
enabled: getStatus(),
click() {getWebviewWebContents().send('switchToColors');}
},
其中 getStatus()
是返回 false
或 true
的函数.
where getStatus()
is function returning false
or true
.
所有这些在 Electron 中都不起作用,因为菜单是在应用程序启动时创建的,根本无法修改.我认为这是一个严重的缺陷,因为动态菜单项非常常见(即:菜单复选框、启用/禁用等).
All this is not working in Electron, as the menu is created at application start and it can't be modified at all. I believe this is a serious lack, as dynamic menu items are very common (i.e.: menu checkboxes, enabled/disabled, etc).
有什么解决方法吗?
推荐答案
我已经通过为菜单项设置一个 Id 来解决这个问题,
I have fixed this by setting an Id to the menu item,
{ label: "Show Colors",
id: 'color-scale',
accelerator: 'CmdOrCtrl+1',
enabled: getStatus(),
click() {getWebviewWebContents().send('switchToColors');}
},
并通过以下方式获取菜单项:
and getting the menu item with:
myItem = menu.getMenuItemById('color-scale')
然后,当我需要以编程方式启用/禁用它时,我正在使用:
Then, when I need to enable/disable it programmatically, I am using:
myItem.enabled = true
或
myItem.enabled = false
这篇关于动态改变 Electron 的菜单项状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!