【问题标题】:BrowserWindow rendering in Electron AppElectron App 中的 BrowserWindow 渲染
【发布时间】:2017-01-30 17:11:54
【问题描述】:



我需要一些关于我正在使用 Electron 构建的应用程序的 JavaScript 专家帮助。问题的核心是,如果我离开主页 (index.html) 并短暂导航回该页面,但 非常 明显地显示了构成垂直的 div 和 ul 元素位于页面左侧的选项卡式菜单(见下图)。

我和我的同事似乎无法弄清楚,也无法在整个网络上找到足够的信息是如何修复或调整这个。一位我们认识的人建议,可能构建某种渲染器来维护在 Main.js 中创建的不同窗口线程,但他不知道如何做到这一点,我们也没有找到有关如何做到这一点的信息。这是正确的路径吗?

或者这仅仅是错误放置或丢失代码的情况,导致页面每次都无法正确加载?

还是其他原因?任何帮助将不胜感激,因为我在解决这个问题时背对着墙。提前谢谢您!

Index.html 渲染:

Index.html 后渲染:


Main.js:

const electron = require('electron');
// Module to control application life.
const {app} = electron;
// Module to create native browser window.
const {BrowserWindow} = electron;
var express = require('express');
var expressapp = express();

var hostname = 'localhost';
var port = 3000;

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

function createWindow() {
// Create the browser window.
win = new BrowserWindow({
width: 1920,
height: 1080,
webPreferences: {
  nodeIntegration: true,
  resizable: true,
  fullscreenable: true,
  maximizable: true,
  minamizable: true,
  movable: true,
  autoHideMenuBar: false,
  allowDisplayingInsecureContent: true,
  allowRunningInsecureContent: true
  }
  })


  // and load the index.html of the app.
  win.loadURL(`file://${__dirname}/index.html`);

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

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

// 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', createWindow);

// 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) {
createWindow();
}
});

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

expressapp.use(express.static(__dirname + '/public'));

expressapp.listen(port, hostname, function() {
console.log(`Server running at http://${hostname}:${port}`);
});


index.html 代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
  <link rel="stylesheet" type:"text/css" href="css/jquery-ui.min.css">
  <link rel="stylesheet" type:"text/css" href="css/bootstrap.min.css">
  <link rel="stylesheet" type:"text/css" href="css/jquery.scrollbar.css">
  <link rel="stylesheet" type:"text/css" href="css/tabs.css">
  <link rel="stylesheet" type:"text/css" href="css/persian-datepicker-0.4.5.min.css">
  <link rel="stylesheet" type:"text/css" href="css/clockpicker.css">
  <link rel="stylesheet" type:"text/css" href="styles.css">
  <link rel="stylesheet" type:"text/css" href="css/entry.css">
  <link rel="stylesheet" type:"text/css" href="css/navbar.css">
  <link rel="stylesheet" type:"text/css" href="css/modal.css">
  <meta id="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
...
<form name="mainForm">
  <div id="sections">
      <ul>
        <li><a href="#section1">Section 1</a></li>
        <li><a href="#section2">Section 2</a></li>
        <li><a href="#section3">Section 3</a></li>
        <li><a href="#section4">Section 4</a></li>
        <li><a href="#section5">Section 5</a></li>
        <li><a href="#section6">Section 6</a></li>
        <li><a href="#section7">Section 7</a></li>
        <li><a href="#section8">Section 8</a></li>
        <li><a href="#section9">Section 9</a></li>
        <li><a href="#section10">Section 10</a></li>
        <li><a href="#section11">Section 11</a></li>
      </ul>
...
  <script>
      window.jQuery = window.$ = require('jquery');
  </script>
  <script src="js/jquery-ui.js"></script>
  <script src="js/bootstrap.min.js"></script>
  <script src="js/jquery.scrollbar.min.js"></script>
  <script src="js/persian-date.min.js"></script>
  <script src="js/persian-datepicker-0.4.5.min.js"></script>
  <script src="js/clockpicker.js"></script>
  <script src="js/jqwidgets/jqxcore.js"></script>
  <script src="js/jqwidgets/jqxexpander.js"></script>
  <script src="js/jqwidgets/jqxinput.js"></script>
  <script src="js/jqwidgets/jqxpasswordinput.js"></script>
  <script src="js/jqwidgets/jqxbuttons.js"></script>
  <script src="js/jqwidgets/jqxvalidator.js"></script>
  <script src="js/data.js"></script>
  <script src="js/model.js"></script>
  <script src="js/view.js"></script>
  <script src="js/form-init.js">
  </script>
</form>
</body>
</html>


更改的浏览器窗口代码:

let win;

function createWindow() {
// Create the browser window.
win = new BrowserWindow({
show: false,
width: 1920,
height: 1080,
backgroundColor: '#4d6b9e',
webPreferences: {
nodeIntegration: true,
resizable: true,
fullscreenable: true,
maximizable: true,
minamizable: true,
movable: true,
autoHideMenuBar: false,
allowDisplayingInsecureContent: true,
allowRunningInsecureContent: true
}
})

win.loadURL(`file://${__dirname}/index.html`);

// and load the index.html of the app.
win.once('ready-to-show', () => {
win.show()
})

【问题讨论】:

    标签: javascript node.js html electron createwindow


    【解决方案1】:

    您的代码示例显示您正在运行 Express 服务器作为主 Electron 进程的一部分:

    var express = require('express');
    var expressapp = express();
    

    根据我的阅读,这是一种不好的做法,因为这意味着您的 Express 服务器必须执行的任何操作都会在服务期间暂时挂起您的 Electron 主进程线程。当您进行小文件加载时,这并不重要,但是任何重要的事情都会使 Electron 主进程无法管理其常规任务(更新电子框架、菜单等——围绕浏览器显示区域的应用程序包装),而它处理任何与 Express 服务器相关的工作(您已经创建并现在在 Electron 主进程中运行)。

    有关此问题的更完整说明,请参见:https://blog.axosoft.com/2016/03/04/electron-things-to-know/

    我还没有找到解决这个问题的好方法,但是将 Express 服务器实例绑定到 Electron 主进程对于任何希望扩展您的应用程序以提供更多文件而不是服务的希望来说都是一颗定时炸弹。

    【讨论】:

      【解决方案2】:

      我无法从您的屏幕截图和描述中看出发生了什么,但您是否尝试过 setting the background color 浏览器窗口?

      另外,如果你的应用只打算在有气隙的机器上运行,那么在 Electron 应用中运行 express 服务器通常是一个坏主意,这将是我的疏忽。

      【讨论】:

      • 感谢您的评论,@Vadim。我很感激考虑。首先,我可能没有很好地描述这个问题。本质上,第二张图片是第一次进入页面时的外观。当您进入时,第一张图片是您短暂看到的(约 1 秒)。选项卡式环境下的项目符号无序列表在不应该显示的时候显示。可悲的是,更改背景颜色并不能解决此问题。其次,我没有听说过在 Electron 中使用 express 服务器。这是为什么?在您发表评论之前,我没有在网上看到任何关于不使用它的信息。
      • @Steve 所以听起来你有一些没有样式的元素被渲染,然后很快就应用了样式。尝试创建一个隐藏的浏览器窗口,然后在它触发 ready-to-show 事件时使其可见。
      • @Steve 至于express... 如果您在应用程序/本地运行express 服务器,这意味着您的所有资源都在本地可用,Electron 可以加载.html 文档和所有相关资源都直接来自磁盘,那么启动 HTTP 服务器有什么意义呢?此外,当用户在浏览器中打开远程网站时,该网站上的 JavaScript 可以访问运行在用户机器上的 HTTP 服务器,请阅读Trend Micro exploit
      • 我已尝试按照您的指定隐藏/显示浏览器窗口,但没有任何影响。我原以为它会首先渲染整个窗口,但加载时没有明显差异。更改后的代码发布在下面。想法?
      猜你喜欢
      • 2017-05-04
      • 1970-01-01
      • 2017-01-29
      • 1970-01-01
      • 1970-01-01
      • 2017-04-03
      • 2015-10-28
      • 2023-04-05
      • 1970-01-01
      相关资源
      最近更新 更多