How to handle popup links in CefSharp

…衆ロ難τιáo~ 提交于 2019-11-29 01:56:51

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)
    {

    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!