隐藏mainWindow时多次调用mainWindow.on('close') [英] mainWindow.on('close') gets called more than one time when I hide mainWindow

查看:503
本文介绍了隐藏mainWindow时多次调用mainWindow.on('close')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个基本的电子托盘,当单击其中一个选项时,它会打开一个窗口。我检查是否使用bool打开了BrowserWindow并创建或显示/隐藏了窗口。

I made a basic electron tray that opens a window when one of it's options is clicked. I check if I opened a BrowserWindow with a bool and create or show/hide the window.

const contextMenu = Menu.buildFromTemplate([
      { label: 'Open configuration menu', click:() => {
        console.log("called createwin");
        createwin();
      }

createwin是:

And createwin is:

function createwin(){
  if (windowshown == false) {
  mainWindow = new BrowserWindow({
    width: 1000,
    height: 800,
    webPreferences: {
      nodeIntegration: true
    }
  })
  console.log("Window has been created")
  windowshown = true;
mainWindow.loadFile('configuration.html')
}
else {
  mainWindow.show(); 
  console.log("Window has been shown");
} 
  mainWindow.on('close', (event) => {
        event.preventDefault();
        console.log("Window has been hidden");
        mainWindow.hide();
    })
}

当我隐藏/显示其创建的窗口时, 4次,我的控制台看起来像这样:

When I hide/show the window it created like 4 times, my console looks like this:


called createwin
Window has been created
window has been hidden
called createwin
Window has been shown
window has been hidden
window has been hidden
called createwin
Window has been shown
window has been hidden
window has been hidden
window has been hidden
called createwin
window has been hidden
window has been hidden
window has been hidden
window has been hidden

第四次后,它只是停止响应。

after fourth time, it just stops responding. I can't even do

      mainWindow.removeAllListeners('close');
      mainWindow.close()
      mainWindow = null
      app.quit();

我的问题是我的应用程序第四次隐藏窗口后完全变得无响应。

My problem is that my app completely becomes unresponsive after hiding the window for fourth time.

更新:如果我以 npm start 而不是Visual Studio代码的调试器开头,则该应用在隐藏窗口后完全无响应。

Update: The app becomes completely unresponsive after hiding the window if I start with npm start instead of visual studio code's debugger.

我在这里缺少什么?

推荐答案

您要添加一个新的每当您调用 createwin 时,事件监听器便会触发 close 事件。然后,在关闭窗口时运行每个事件处理程序,从而导致重复的窗口已被隐藏

You're adding a new event listener to the close event whenever you call createwin. Each of the event handlers is then run when the window is closed, resulting in the repeated window has been hidden.

创建新窗口时,只需添加一次事件处理程序:

You only need to add the event handler once, when you create the new window:

function createwin(){
    if (windowshown == false) {
        mainWindow = new BrowserWindow({
            width: 1000,
            height: 800,
            webPreferences: {
                nodeIntegration: true
            }
        })
        console.log("Window has been created")
        windowshown = true;
        mainWindow.loadFile('configuration.html')

        mainWindow.on('close', (event) => {
            event.preventDefault();
            console.log("Window has been hidden");
            mainWindow.hide();
        })
    }
    else {
        mainWindow.show();
        console.log("Window has been shown");
    }
}

这篇关于隐藏mainWindow时多次调用mainWindow.on('close')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆