mirror of
https://github.com/ElectronNET/Electron.NET.git
synced 2026-02-03 21:25:13 +00:00
Add EventArgs to BrowserWindow.OnClose event to prevent closing the window #880
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @niklasstich on GitHub (Apr 21, 2023).
Currently, the
OnCloseevent onBrowserWindowdoes not provide any event args to callees of the event. This makes it impossible to prevent closing the window from C# code directly without involving Javascript that catches the 'onbeforeunload' event.This fact was mentioned in several previous issues:
https://github.com/ElectronNET/Electron.NET/issues/391#issuecomment-719928364
https://github.com/ElectronNET/Electron.NET/issues/567
I would therefore request that EventArgs be added to this event that allow preventing the window from being closed, analogous to the Electron BrowserWindow
closeevent: https://www.electronjs.org/docs/latest/api/browser-window#event-close@ssrdop commented on GitHub (Jun 5, 2023):
Hello! OnClose event still doesnot has event args to prevent closing. I know how to prevent App closing, but browserWindow still doesnot has this feature.
At https://github.com/ElectronNET/Electron.NET/issues/567#issuecomment-1487165152 u said "With native Electron 23 and .NET 6 support. Your problem should be fixed here. " but seems it doesnot. Can u explain, pls?
@terryjbutler commented on GitHub (Jun 19, 2023):
Good day! I've also been facing this problem. Any information on this would be very appreciated.
@ssrdop commented on GitHub (Jun 20, 2023):
Hey ive found some solution - u can use window.onbeforeunload
so when ur app starts u can add ur callback (even async) to window.onbeforeunload
I have react app, so i wrote something like this:
So u can make it with any javasript framework, react just for example
It prevents closing app and it always calls ur callback function u wrote inside window.onbeforeunload.
But there are some problem - even if u want to close app now - u cant.
But for this problem i also have a solution - if u know that u want to close ur app then do next:
window.onbeforeunload = null
and call after i:
Electron.App.Quit(); // inside ur back, using websockets or via http api, doesnot matter.
How it works - when ur app starts u assing to window.onbeforeunload some callback thats calling everytime ur application or window is closing and its prevents closing at all (event if u press alt+f4, or from window toolbar). So often u assign this callback for window.onbeforeunload when u want to save some data before or ask user confirmation or something else. If u did this job and u ready to close app - just doint "window.onbeforeunload = null" resets any callback on closing and u need to call "Electron.App.Quit();" and after it there are no callback for window.onbeforeunload and ur app closing.
Sorry i am not native english speaker, but i hope u will understand.
@FlorianRappl commented on GitHub (Nov 1, 2025):
This is not possible as the
preventDefaultis a method, i.e., it cannot be serialized. Even if it could (or we would allow calling it indirectly) it would not be possible - the bridge is async and therefore the event has been fully processed once the method would actually be called.