【问题标题】:Electron best way for multiple windows多个窗口的电子最佳方式
【发布时间】:2017-06-24 16:26:46
【问题描述】:

打开多个 BrowserWindows 的正确方法是什么?我这样做并且到目前为止工作正常。是不是更好地做出一系列胜利?

let win;

function createWindow () {
  for (i=0; i<loadNotes.notes.length; i++){
  win = new BrowserWindow({
    'x': loadNotes.notes[i].pos.x,
    'y': loadNotes.notes[i].pos.y,
    'width': loadNotes.notes[i].pos.width,
    'height': loadNotes.notes[i].pos.height,
    'frame': false});

  win.setMenu(null);
  win.loadURL(`file://${__dirname}/index.html?${loadNotes.notes[i].name}`);
  //win.webContents.openDevTools()
  }
  win.on('close', () => {

  })

  win.on('closed', () => {
    win = null
  });
}

【问题讨论】:

    标签: electron


    【解决方案1】:

    这取决于你是否要使用win 的实例方法。如果没有,您可以保留您的代码。 只有一个建议,为了获得最佳用户体验,建议您优雅地显示您的窗口

    win = new BrowserWindow({
        ...., 
        show: false})
    ...
    win.loadURL(`file://${__dirname}/index.html?${loadNotes.notes[i].name}`)
    win.once('ready-to-show', () => {
      win.show()
    })
    

    【讨论】:

    • 你好,优雅版比原版做了什么?
    【解决方案2】:

    在电子中创建多窗口应用程序很容易。这里有一个 nodejs 模块,可以帮助您轻松创建多窗口应用程序。

    您只需执行以下命令即可安装此模块

    npm install --save electron-window-manager

    然后,在应用程序 main.js(或您为应用程序选择的任何内容)内,需要该模块,如下所示:

    var windowManager = require('electron-window-manager');

    在主进程中你可以这样使用它:

    const electron = require('electron');
    const app = electron.app;
    const windowManager = require('electron-window-manager');
    
    // When the application is ready
    app.on('ready', function(){
        windowManager.init( ... );
        // Open a window
        windowManager.open('home', 'Welcome ...', '/home.html');
    });
    

    并且在渲染器进程中(在创建的窗口内),你可以这样使用它:

        var remote = require('remote');
        var windowManager = remote.require('electron-window-manager');
    
        // Create a new window
        var win2 = windowManager.createNew('win2', 'Windows #2');
        win2.loadURL('/win2.html');
        win2.onReady( ... );
        win2.open();
    

    请继续查看模块代码,并亲自看看它是如何工作的,没有任何魔法,但它是一致且易于阅读的。看看代码就可以了。

    这是一个关于这个模块的完整指南,希望对你有所帮助

    https://github.com/TamkeenLMS/electron-window-manager

    【讨论】:

      猜你喜欢
      • 2011-09-21
      • 2013-06-22
      • 1970-01-01
      • 2019-12-18
      • 1970-01-01
      • 1970-01-01
      • 2011-02-20
      • 2014-10-30
      • 2014-04-22
      相关资源
      最近更新 更多