問題描述
與任何標(biāo)準(zhǔn)原生應(yīng)用程序一樣,我的電子應(yīng)用程序也需要根據(jù)實(shí)時(shí)使用結(jié)果更改多個(gè)菜單項(xiàng)的狀態(tài)(啟用/禁用).
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 中設(shè)置我的菜單:
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
的函數(shù).
where getStatus()
is function returning false
or true
.
所有這些在 Electron 中都不起作用,因?yàn)椴藛问窃趹?yīng)用程序啟動(dòng)時(shí)創(chuàng)建的,根本無法修改.我認(rèn)為這是一個(gè)嚴(yán)重的缺陷,因?yàn)閯?dòng)態(tài)菜單項(xiàng)非常常見(即:菜單復(fù)選框、啟用/禁用等).
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).
有什么解決方法嗎?
推薦答案
我已經(jīng)通過為菜單項(xiàng)設(shè)置一個(gè) Id 來解決這個(gè)問題,
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');}
},
并通過以下方式獲取菜單項(xiàng):
and getting the menu item with:
myItem = menu.getMenuItemById('color-scale')
然后,當(dāng)我需要以編程方式啟用/禁用它時(shí),我正在使用:
Then, when I need to enable/disable it programmatically, I am using:
myItem.enabled = true
或
myItem.enabled = false
這篇關(guān)于動(dòng)態(tài)改變 Electron 的菜單項(xiàng)狀態(tài)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!