ChromiumWebBrowser
has a LifeSpanHandler
property. To manually control popup windows in Cefsharp, you have to implement your own life span handler object implementing the ILifeSpanHandle
interface.
Every time browser wants to open a new window, it will call your life span handler's OnBeforePopup
function. Here you can implement your desired behavior. If you return false
, browser will pop up a new window. If you return true
, browser will ignore pop up action, but you can manually create your new window, new tab, etc...
This is a very simple example of custom life span hander. On popup request, it fires an event called PopupRequest. You can subscribe such event and create new window/tab manually. Then, it returns true that instructs ChromiumWebBrowser
not to create its own new window. However, you need to implement creating new window with another ChromiumWebBrowser
on your own.
public class SampleLifeSpanHandler: ILifeSpanHandler
{
public event Action<string> PopupRequest;
public bool OnBeforePopup(IWebBrowser browser, string sourceUrl, string targetUrl, ref int x, ref int y, ref int width,
ref int height)
{
if (PopupRequest != null)
PopupRequest(targetUrl);
return true;
}
public void OnBeforeClose(IWebBrowser browser)
{
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…