为什么关闭渲染器窗口时我的Electron应用程序退出? [英] Why does my Electron app quit when closing a Renderer window?

查看:995
本文介绍了为什么关闭渲染器窗口时我的Electron应用程序退出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Windows上进行测试。
该应用程序在就绪上设置了任务栏菜单,并带有关于标签。单击时,它显示一个BrowserWindow:

I'm testing on Windows. The app sets up a Tray menu on 'ready', with an 'About' label. When clicked, it shows a BrowserWindow:

 var aboutBox = new BrowserWindow({
        width: 460, height: 176, useContentSize: true,
        icon: iconImg,
        maximizable: false, fullscreenable: false, resizable: false, minimizable: false
    });

然后,当用户单击确定时,用以下命令将其关闭:

and then, when the user clicks on OK, closing it with:

const remote = require('electron').remote;
remote.getCurrentWindow().close();

导致应用退出。

为什么?

推荐答案

在您的main.js中,您可能拥有这段代码:

In your main.js you could have this code:

// Quit when all windows are closed.

  app.on('window-all-closed', function () {
  // On OS X it is common for applications and their menu bar  
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

因此,由于您关闭了唯一窗口,因此会触发该事件并关闭了应用程序。

So because you close the unique windows this event it's emitted and app is closed.

编辑
此行为在Electron中也是默认设置,因此为避免关闭应用程序而关闭主窗口,请添加以下行:

EDIT This behavior is also default in Electron, so to avoid closing app closing the main window add this line:

app.on('window-all-closed', e => e.preventDefault() )

这篇关于为什么关闭渲染器窗口时我的Electron应用程序退出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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