- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 IPCMain
和 IPCRenderer
通过打开新窗口并关闭旧窗口来切换到 3 个不同的窗口,我错过了一些东西,并且很难弄清楚。
基本上,用户会选择并单击第 3 个类别,相应的窗口将出现,当前窗口将关闭。
选择期间的 switch/case 语句:
$('.category a').on('click',function(){
var cat = $(this).attr('href');
const {
ipcRenderer
} = require('electron');
switch (cat){
case 'Window_1':
ipcRenderer.send('newWindow', 'Window_1');
break;
case 'Window_2':
ipcRenderer.send('newWindow', 'Window_2');
break;
case 'Window_3':
ipcRenderer.send('newWindow', 'Window_3');
break;
default:
break;
}
});
这是显示窗口的代码
//some code for declaration of library and variables
//....
let {
ipcMain
} = electron;
var correspondingWindow = null;
let mainWindow;
app.on('ready', function () {
//Some code for Initialization of Main Window...
});
//function that will be call to show a new window and close the other one
ipcMain.on('newWindow', function (e, category) {
var height;
var width;
var address;
//1: focus on the new window
if (correspondingWindow) {
correspondingWindow.focus();
return;
}
//2: initialize the height, width and the file address of new Window
switch (category){
case 'Window_1':
height = 600;
width = 800;
address = '../html/window_1.html';
break;
case 'Window_2':
height = 600;
width = 400;
address = '../html/window_2.html';
break;
case 'Window_3':
height = 600;
width = 600;
address = '../html/window_3.html';
break;
default:
break;
}
//3: Initialization of new Window
correspondingWindow = new BrowserWindow({
height: height,
width: width,
minHeight: height,
minWidth: width,
icon: __dirname + iconPath,
frame: false,
backgroundColor: '#FFF',
show: false
});
//4: Load HTML into Window
correspondingWindow.loadURL(url.format({
pathname: path.join(__dirname, directoryHtml + address),
protocol: 'file',
slashes: true
}));
//5: Initialization closing event of Window
correspondingWindow.on('closed', function () {
correspondingWindow = null;
});
//6: finally, show it once its ready
correspondingWindow.once('ready-to-show', () => {
correspondingWindow.show();
});
//7: Close the Main Window if possible, so that only one window will show at a time.
if (mainWindow !== null) {
mainWindow.close();
mainWindow = null;
}
});
我能够显示新窗口并关闭 MainWindow
,但不能通过再次调用 ipcMain
函数来第二次关闭。第二次调用时没有任何反应。
我可以通过创建三个不同的 ipcMain
函数来使其工作,但我想找到一种方法使其只有一个函数。
最佳答案
据我了解,您希望基于单击的项目打开一个新的 BrowserWindow 来代替当前的 BrowserWindow,并且您希望通过创建一个可以实现这一目的的函数来缩短代码。
你足够亲密的伙伴。
实际上,您的点击事件中不需要条件语句,但如果您选择,也可以,我注意到您使用 JQuery
,因此这将是您单击的元素上的代码:
<script src="../js/renderer.js"></script>
<script>window.$ = window.jQuery = require('jquery');</script>
<script>
$('.category a').on('click',function(){
const {
ipcRenderer
} = require('electron');
ipcRenderer.send('createBrowserWindow', $(this).attr('href'));
const remote = require('electron').remote;
remote.getCurrentWindow().close();
});
</script>
请注意,我使用了remote
函数。
这是你的main.js
const {
app,
BrowserWindow
} = electron;
let {
ipcMain
} = electron;
let mainWindow;
/*1*/
var toQuit = true; //very important
var category = 'window_1'; //default category
/*2*/
app.on('ready', function () {
"use strict";
createWindow(); //call this function to create a BrowserWindow on its lunch
});
app.on('closed', function () {
"use strict";
mainWindow = null;
});
app.on('window-all-closed', function () {
"use strict";
if (process.platform !== 'darwin') {
app.quit();
}
});
/*3*/
function createWindow() {
"use strict";
var height;
var width;
var address;
switch (category) {
case 'window_1':
height = 600; //Initialize size of BrowserWindow
width = 400; //Initialize size of BrowserWindow
address = '../html/window_1.html'; //Initialize the address or the file location of your HTML
break;
case 'window_2':
height = 600; //Initialize size of BrowserWindow
width = 400; //Initialize size of BrowserWindow
address = '../html/window_1.html'; //Initialize the address or the file location of your HTML
break;
case 'window_3':
height = 600; //Initialize size of BrowserWindow
width = 400; //Initialize size of BrowserWindow
address = '../html/window_1.html'; //Initialize the address or the file location of your HTML
break;
default:
break;
}
mainWindow = new BrowserWindow({
height: height, //height and width of BrowserWindow
width: width, //height and width of BrowserWindow
minHeight: height, //minimum height and width of BrowserWindow, you can remove this if you want
minWidth: width, //minimum height and width of BrowserWindow, you can remove this if you want
icon: __dirname + iconPath,
frame: false,
backgroundColor: '#FFF',
show: false
});
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, address), //file location of html
protocol: 'file',
slashes: true
}));
mainWindow.once('ready-to-show', () => {
mainWindow.show(); //we only want to show it when its ready to avoid the FLASH WHITE during lunch of BrowserWindow
mainWindow.focus(); //We make sure to focus on it after showing
});
/**The magic start here, **/
mainWindow.on('closed', (e) => {
e.preventDefault(); //We have to prevent the closed event from doing it.
if(toQuit){ //if the value of toQuit is true, then we want to quit the entire application
mainWindow = null;
app.exit(); //quit or exit the entire application
}else{
mainWindow.hide(); //just hide the BrowserWindow and don't quit the entire application
toQuit = true; //reset the value of toQuit to true
}
});
}
//call this function from your any Javascript
ipcMain.on('createBrowserWindow', function (e, cat) {
"use strict";
category = cat; //get the category of window on which we want to show
toQuit = false; //set the value to false, meaning we don't want to close the entire application but just hide the current BrowserWindow
createWindow(); //call this function over and over again
});
事情是如何发生的:
我们启动一个 BrowserWindow
,其默认类别是 window1
。
在您的 Javascript 中,我们使用 ipcMain
和 ipcRenderer
来调用 main.js
中的函数,然后使用 remote
来关闭 BrowserWindow
但在关闭事件期间我们阻止它退出 BrowserWindow,而是隐藏它。
希望有帮助。
关于javascript - 打开一个新窗口并关闭另一个窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53853632/
我正在尝试使用特定路线打开一个新窗口,但似乎路线页面未正确加载。将从FrmApprovalMain.js打开新的 Electron 窗口,并应使用FrmPOdet.js打开新窗口。但是,当它打开时,会
我对 Tkinter 比较陌生,我需要帮助。 当从父窗口单击按钮时,我创建了一个新窗口。新窗口是 def new_Window。但我似乎无法获取窗口中的信息,如下编码: from tkinter im
我正在尝试使用 Javascript 从空白 iframe 中创建一个重定向,该 iframe 将定向到新窗口或选项卡中的 URL。 更具体地说,我试图让一个 Facebook 标签指向我公司的网页,
在问这个问题之前,我确实搜索了很多。抱歉,如果这是一个简单的问题。 在我的 wxWidgets 应用程序中,我想从菜单选项中新建一个 wxHtmlWindow。 (我已经正确显示了我的主应用程序窗口)
我认为我不是很了解这个,因为我对 jquery 还很陌生。我有一个隐藏了 3 个食谱的页面。单击食谱 A 的链接时,它会以模式打开。我希望能够只打印食谱的内容。所以我打开一个新窗口(非模态)并尝试将食
我想在带有可滚动文本框的现有主 Windwoe 旁边创建一个新窗口。 我在主窗口中按下“打开新窗口”按钮,然后它应该会打开一个带有可滚动文本框的新窗口。 在 form2 中 在 WPF 中,您可以在主
我正在使用 Simple Icons 制作自定义 Pinterest Pin It 按钮,我正在使用 Pinterest Widget Builder 给我的链接。在默认的 Pin It 按钮上,它会
我的表单上出现一个确认对话框,单击“确定”后会将用户重定向到另一个页面。 但是,是否可以在新窗口中打开该页面(例如 target="_blank") 我正在使用的代码是: var answer=con
相关但处于休眠状态:Javascript to open URL within a new Tab和其他一些...... 我有一个客户坚持认为特定链接需要在新选项卡中打开,而不是在窗口中打开。它由表单
我遇到了 Android 问题 WebView , 我想用 target='_blank' 打开一个 URL在同一WebView ,就像所有其他的一样URLs正在开放。 另请注意,我重写了 WebVi
可以从 Controller 重定向到某个 url 并同时在新窗口中打开该 url 吗? 最佳答案 不是真的来自 Controller Action 。 重定向是带有 301/302 状态代码和 Ra
在 Electron vue 中,我想为每个新窗口创建新的 vue 实例。这工作正常,直到我想将路由器子组件加载到新窗口。例如我有条目 add: path.join(__dirname, '../sr
案例1: var printWindow = window.open("", "print_window"); if(printWindow) { printWindow.addEventLi
我需要使用 Javascript Window.Open 函数打开一个新的 (_blank) 窗口。 URL 编码的回车符/换行符 (%0A) 似乎不起作用。有谁知道解决这个问题?例如,我有以下 UR
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 1年前关闭。 Imp
当 flash 有键盘焦点时,CTRL+T(新标签)和 CTRL+N(新标签)窗口)被闪光拦截。 有没有办法将这些事件传递给浏览器以便它们工作(打开新选项卡,打开新浏览器)或者是否有用于这些操作的 j
我有一个 ASP.Net MVC 页面,上面有一个非常简单的表单:一个文本框和一个提交按钮。该表单使用标准的 mvc BeginForm() 语法: 当直接打开页面时,一切正常。 但是,我们有另一个
如果您对此问题感兴趣,请阅读下面的更新 #2 ;) 假设我将此代码放入我的扩展程序的 JS 中。 var reader = { onInputStreamReady : function(in
我是一名优秀的程序员,十分优秀!