无论我做啥,我的 Electron 窗口都不会出现,我的代码有问题吗?

Posted

技术标签:

【中文标题】无论我做啥,我的 Electron 窗口都不会出现,我的代码有问题吗?【英文标题】:My window in Electron doesn't appear no matter what I do, there is something wrong with my code?无论我做什么,我的 Electron 窗口都不会出现,我的代码有问题吗? 【发布时间】:2021-08-21 00:08:42 【问题描述】:

首先我使用的是 Vuejs 和 Electron,package.json 将在本文末尾。

我正在尝试使用此侦听器来按照 Electron 文档的建议显示我的窗口。

win.once("ready-to-show", () => win.show());

但这似乎从未触发,只有当我手动获取变量 win 并调用函数 show() 时。

下面是我的 background.js(运行 Electron 主进程的文件)文件和我的 windowManager.js 文件(我为构建我的窗口并返回它们而创建的文件,我将此文件导入到 background.js )

//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);

//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,
        height: 550,
      ,
      
        frame: false, //Remove frame to hide default menu
        show: false, //Don't show at the begin
      ,
      "main"
    );
    //Extra window behavior
    win.maximize();
    return win;
  ,
;

还有 package.json


  "name": "simulans",
  "version": "0.1.0",
  "private": true,
  "scripts": 
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint",
    "electron:build": "vue-cli-service electron:build",
    "electron:serve": "vue-cli-service electron:serve",
    "postinstall": "electron-builder install-app-deps",
    "postuninstall": "electron-builder install-app-deps"
  ,
  "main": "background.js",
  "dependencies": 
    "core-js": "^3.6.5",
    "vue": "^2.6.11",
    "vue-router": "^3.0.3",
    "vuex": "^3.4.0"
  ,
  "devDependencies": 
    "@vue/cli-plugin-babel": "~4.5.0",
    "@vue/cli-plugin-eslint": "~4.5.0",
    "@vue/cli-plugin-vuex": "~4.5.0",
    "@vue/cli-service": "^3.0.5",
    "@vue/eslint-config-prettier": "^6.0.0",
    "babel-eslint": "^10.1.0",
    "electron": "^13.0.1",
    "electron-devtools-installer": "^3.1.0",
    "eslint": "^6.7.2",
    "eslint-plugin-prettier": "^3.3.1",
    "eslint-plugin-vue": "^6.2.2",
    "prettier": "^2.2.1",
    "vue-cli-plugin-electron-builder": "~2.0.0",
    "vue-template-compiler": "^2.6.11"
  ,
  "eslintConfig": 
    "root": true,
    "env": 
      "node": true
    ,
    "extends": [
      "plugin:vue/essential",
      "eslint:recommended",
      "@vue/prettier"
    ],
    "parserOptions": 
      "parser": "babel-eslint"
    ,
    "rules": 
      "prettier/prettier": 0
    
  ,
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not dead"
  ]

【问题讨论】:

一切似乎都是正确的,我唯一的建议是完成消除过程。注释掉事情,直到它几乎可以工作。可以尝试的一些事情是:尝试加载 google.com 而不是您的 URL,创建一个最小的错误示例。 你为什么要等待 browserwindow 构造函数?也许在添加准备显示处理程序之前有什么东西被抛出? @Joshua 我试过了,但没有任何结果。 @pushkin 我犯了一个错误,只有函数win.loadURL返回了一个Promise,BrowserWindow的构造函数没有返回一个Promise,你是对的,但是我现在已经改变了它并且问题还在继续。我将更新上面的代码,在新的 BrowserWindow 之前取消等待。 【参考方案1】:

您正在使用异步函数来创建浏览器窗口。这不是问题,但在这里,您在您进行异步调用之后绑定您的 BrowserWindow 事件。

这基本上意味着您在绑定您的ready-to-show 事件之前正在等待您的try/catch 块结束。我怀疑事件是在异步调用结束之前触发的。

如果是这种情况,解决方案就是将您的 win.once("ready-to-show", () => win.show()); 放在您的 try ... catch ... 块之前。

【讨论】:

以上是关于无论我做啥,我的 Electron 窗口都不会出现,我的代码有问题吗?的主要内容,如果未能解决你的问题,请参考以下文章

应用 CIFilter 后,无论我做啥,图像都会变大

无论我做啥,变量都是未定义的[重复]

无论我做啥,都可以保存 Visual Studio Code 格式。我该如何阻止这个?

无论我做啥,PayPal IPN 都会不断返回“无效”代码

NPM,无论我做啥,都找不到'graceful-fs'

无论我做啥[重复],VSCode Python版本在集成终端中默认为2.7