如何解决无论我做什么,我的 Electron 窗口都没有出现,我的代码有问题吗?
首先我使用 Vuejs 和 Electron,package.json 将在这篇文章的末尾。
我正在尝试按照 Electron 文档的建议使用此侦听器来显示我的窗口。
service_ids[1]=1 service_names[1]=foo
service_ids[2]=2 service_names[2]=bar
但似乎这从未被触发,只有当我手动获取变量 win 并调用函数 show() 时。
下面是我的 background.js(运行 Electron 主进程的文件)文件和我的 windowManager.js 文件(我为构建我的窗口而创建的文件并返回它们,我将此文件导入 background.js )
win.once("ready-to-show",() => win.show());
//background.js
"use strict";
import { app,protocol,BrowserWindow,ipcMain } from "electron";
import installExtension,{ VUEJS_DEVTOOLS } from "electron-devtools-installer";
import { windowManager } from "./backend/windowManager.js";
const isDevelopment = process.env.NODE_ENV !== "production";
let winHome,winMain;
// Scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([
{ scheme: "app",privileges: { secure: true,standard: true } },]);
// 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",async () => {
// 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 (BrowserWindow.getAllWindows().length === 0) {
winHome = await windowManager.createHomeWindow();
winHome.show();
}
});
// 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",async () => {
if (isDevelopment && !process.env.IS_TEST) {
// Install Vue Devtools
try {
await installExtension(VUEJS_DEVTOOLS);
} catch (e) {
console.error("Vue Devtools failed to install:",e.toString());
}
}
winHome = await windowManager.createHomeWindow();
winHome.show();
});
// Exit cleanly on request from parent process in development mode.
if (isDevelopment) {
if (process.platform === "win32") {
process.on("message",(data) => {
if (data === "graceful-exit") {
app.quit();
}
});
} else {
process.on("SIGTERM",() => {
app.quit();
});
}
}
// IPC callback listeners
const closeWindow = () => {
BrowserWindow.getFocusedWindow().close();
};
const openMainWindow = async () => {
winMain = await windowManager.createMainWindow();
winHome.close();
};
// Setting the IPC listeners
ipcMain.on("close-window",closeWindow);
ipcMain.on("open-main-window",openMainWindow);
和 package.json
//windowManager.js
import { BrowserWindow } from "electron";
import { createProtocol } from "vue-cli-plugin-electron-builder/lib";
import * as path from "path";
export const windowManager = {
async createWindow(sizeProperties,settings,router = null) {
let win;
// Create the browser window.
win = new BrowserWindow({
...sizeProperties,...settings,webPreferences: {
// Use pluginOptions.nodeIntegration,leave this alone
// See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION,preload: path.join(__dirname,"preload.js"),},});
//Checking with was sent some router
router = router ? `/#/${router}` : "";
try {
if (process.env.WEBPACK_DEV_SERVER_URL) {
// Load the url of the dev server if in development mode
await win.loadURL(`${process.env.WEBPACK_DEV_SERVER_URL}${router}`);
if (!process.env.IS_TEST) win.webContents.openDevTools();
} else {
createProtocol("app");
// Load the index.html when not in development
await win.loadURL(`app://./index.html${router}`);
}
} catch (error) {
console.error(error);
}
//Setting the default behavior for window
win.once("ready-to-show",() => {
win.show();
});
win.on("closed",() => {
win.destroy();
});
//TODO Check if this line is necessary
win.show();
return win;
},async createHomeWindow() {
return await this.createWindow(
{
width: 706,height: 550,{
frame: false,//Remove frame to hide default menu
resizable: false,show: false,//Don't show at the begin
}
);
},async createMainWindow() {
let win = await this.createWindow(
{
width: 800,//Remove frame to hide default menu
show: false,//Don't show at the begin
},"main"
);
//Extra window behavior
win.maximize();
return win;
},};
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。