问题
I'm using CefSharp.WinForms.ChromiumWebBrowser v41.0.1 in my project. There are many tabs, each tab has a chromium webbrowser. I try to close one of these tabs by javascript function window.close(). But it also closes both all tabs and my program. What is the main reason? And is there any way to close only one tab by javascript?
回答1:
I believe you'll find that CEF
sends a WM_CLOSE
message to the top level window as it's default behavior.
One option is to upgrade to the 43.0.0-pre02
release, then implement ILifeSpanHandler.DoClose
for when you should be able to return true
to implement your own custom behavior.
https://github.com/cefsharp/CefSharp/blob/cefsharp/43/CefSharp/ILifeSpanHandler.cs#L109
http://magpcss.org/ceforum/apidocs3/projects/%28default%29/CefLifeSpanHandler.html#DoClose%28CefRefPtr%3CCefBrowser%3E%29
You could possibly hook into window.close
, bind it to a method using Javascript binding
, then handle the close yourself.
The third option, is to look at somehow canceling the form close, maybe with the FormClosing
event.
I haven't tried any of these options explicitly, to know which is the easiest and cleanest, I'm sure you can find something that works though.
回答2:
I've implemented the interface ILifeSpanHandler to create a new tab and close it by javascript. However, the tabs opened by javascript can not be closed by javascript, it lead to closing all tabs, programs and throw a null references exception. I tried to create a new tab by another thread, and similar errors have occurred, while the tabs created by the main thread can be normally closed by window.close javscript. I realize that creating a new tab by javascript also be implemented in a particular thread. Thus, the closure by javascript of chromium webbrowser created in another thread will cause errors . How can I handle it?
来源:https://stackoverflow.com/questions/32583742/cefsharp-winforms-close-tab-by-javascript