gpt4 book ai didi

javascript - Electron - 关闭初始窗口但让 child 保持打开状态

转载 作者:行者123 更新时间:2023-11-30 20:50:38 24 4
gpt4 key购买 nike

我正在编写一个 electron 应用程序,它应该加载启动画面,然后打开一个新窗口。之后应关闭启动画面。

但是我做不到。在我的启动脚本 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 中。但这导致了以下错误:

Attempting to call a function in a renderer window that has been closed or released.

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)

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

最佳答案

我通常使用不同的方法。以下是使用方法:

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

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

   '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.*/

关于javascript - Electron - 关闭初始窗口但让 child 保持打开状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48224116/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com