【问题标题】:Electron - Close initial window but keep child open电子 - 关闭初始窗口但保持孩子打开
【发布时间】:2019-10-07 00:20:32
【问题描述】:

我正在编写一个应该加载初始屏幕的电子应用程序,然后打开一个新窗口。之后,启动画面应关闭。

但是我无法做到这一点。在我的index.js 启动脚本中,我有以下代码:

const { app, BrowserWindow } = require("electron");

app.on("ready", () => {
    let win = new BrowserWindow({ /*...*/ });
    win.loadURL(`file://${__dirname}/splash/splash.html`);
    win.on("ready-to-show", () => { win.show(); });
    win.on("closed",        () => { app.quit(); });
});

splash.html 中,我通过使用加载splash.js

<script>require("./splash");</script>

在 splash.js 中我尝试了以下代码:

const remote = require("electron").remote;

let tWin = remote.getCurrentWindow();

let next = function(){ 
    let win = new remote.BrowserWindow({ /*...*/ });
    win.loadURL(`file://${__dirname}/main/main.html`);
    win.on("ready-to-show", () => { win.show(); });
    win.on("closed",        () => { app.quit(); });

    tWin.close();

    // I could just use win.hide(); here instead 
    // of tWin.close(); but that can't really be the right way.
};

函数next() 在超时后被调用。问题是,如果调用,主窗口会显示一秒钟,但 slpash 和 main 都会立即关闭。

我试图通过注释掉来解决这个问题

win.on("closed", () => { app.quit(); });

在我的index.js。但这导致了以下错误:

试图在已关闭或释放的渲染器窗口中调用函数。

Uncaught Exception:
Error: Attempting to call a function in a renderer window that has been closed or released. Function provided here: splash.js:38:9.
    at BrowserWindow.callIntoRenderer (/usr/lib/node_modules/electron-prebuilt/dist/resources/electron.asar/browser/rpc-server.js:199:19)
    at emitOne (events.js:96:13)
    at BrowserWindow.emit (events.js:188:7)

有人知道如何防止新创建的窗口关闭吗?

【问题讨论】:

  • 为什么要在另一个窗口中加载启动画面。你不喜欢那个代码吗?
  • 你应该在同一个进程中加载​​'splash'和'main'窗口,然后根据你需要的事件显示/隐藏,我会用我的代码示例发布答案。

标签: javascript node.js electron


【解决方案1】:

我通常使用不同的方法。下面是一个用法:

  1. 为主窗口和启动窗口创建全局变量引用,否则将由垃圾收集器自行关闭。
  2. 加载“splash”浏览器窗口
  3. 在 'show' 事件中,我调用了一个函数来加载 'main' 窗口
  4. main 窗口“dom-ready”上,我关闭了“splash”并显示“主要'

这是我的 main.js 电子代码的一个示例,欢迎提问:

   'use strict';

    //generic modules
    const { app, BrowserWindow, Menu } = require('electron');
    const path = require('path')
    const url = require('url')

    const config = require('./config'); //                              => 1: archivo de configuracion
    const fileToLoad = config.files.current ? config.files.current : config.files.raw;
    const jsonData = require(fileToLoad); //                            => 2: archivo de datos (json) 
    const pug = require('electron-pug')({ pretty: true }, jsonData); // => 3: pasamos datos ya tratados a plantillas pug/jade 


    // Keep a global reference of the window object, if you don't, the window will
    // be closed automatically when the JavaScript object is garbage collected.
    let win, loading
    app.mainWindow = win;

    function initApp() {
        showLoading(initPresentation)
    }

    function showLoading(callback) {
        loading = new BrowserWindow({ show: false, frame: false })
        loading.once('show', callback);
        loading.loadURL(url.format({
            pathname: path.join(__dirname, '/src/pages/loading.html'),
            protocol: 'file:',
            slashes: true
        }))

        loading.show();
    }

    function initPresentation() {
        win = new BrowserWindow({
            width: 1280,
            height: 920,
            show: false,
            webPreferences: {
                experimentalFeatures: true
            }
        })
        win.webContents.once('dom-ready', () => {
                console.log("main loaded!!")
                win.setMenu(null);
                win.show();
                loading.hide();
                loading.close();
            })
            // Emitted when the window is closed.
        win.on('closed', () => {
            // Dereference the window object, usually you would store windows
            // in an array if your app supports multi windows, this is the time
            // when you should delete the corresponding element.
            win = null
        })
        win.loadURL(url.format({
            //pathname: path.join(__dirname, '/src/pages/home.pug'),
            pathname: path.join(__dirname, '/lab/pug/index.pug'),
            protocol: 'file:',
            slashes: true
        }))

        win.webContents.openDevTools() // Open the DevTools.
    }

    // This method will be called when Electron has finished
    // initialization and is ready to create browser windows.
    // Some APIs can only be used after this event occurs.
    app.on('ready', initApp)

    // Quit when all windows are closed.
    app.on('window-all-closed', () => {
        // On macOS 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()
        }
    })

    app.on('activate', () => {
        // On macOS it's common to re-create a window in the app when the
        // dock icon is clicked and there are no other windows open.
        if (win === null) {
            initApp()
        }
    })

    // In this file you can include the rest of your app's specific main process
    // code. You can also put them in separate files and require them here.*/

【讨论】:

  • 这太棒了。非常感谢!它就像一个魅力:)
猜你喜欢
  • 1970-01-01
  • 2012-05-22
  • 1970-01-01
  • 2015-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多