繁体   English   中英

BrowserWindow在Electron App中渲染

[英]BrowserWindow rendering in Electron App



对于我使用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()
})

我无法确定你的截图和描述中发生了什么,但是你尝试过设置浏览器窗口的背景颜色吗?

此外,如果不在电子应用中运行快速服务器通常是一个坏主意,除非您的应用程序仅用于在气隙机器上运行,否则我将失职。

您的代码示例显示您正在运行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主进程是一个时间炸弹,可以扩展你的应用程序,而不仅仅是提供一些文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM