码迷,mamicode.com
首页 > 其他好文 > 详细

CEF Xilium.CefGlue 在当前窗口中打开所有链接(防止弹窗)

时间:2015-07-18 22:46:52      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:cef xilium.cefglue在当   xilium.cefglue防止弹窗   

我们在使用Xilium.CefGlue编写浏览器应用程序时,对于嵌入的网页如果有链接会在新窗口打开,这样的用户体验会很差,因此我们需要修改程序,使所有链接都在当前窗口中打开。


首先引用Xilium.CefGlue.dll文件,需要继承Xilium.CefGlue 中的CefLifeSpanHandler类


namespace Xilium.CefGlue
{
    // 摘要:
    //     Implement this interface to handle events related to browser life span. The
    //     methods of this class will be called on the UI thread unless otherwise indicated.
    public abstract class CefLifeSpanHandler


继承CefLifeSpanHandler类,并重写OnBeforePopup方法,实现如下:


internal sealed class CwbLifeSpanHandler : CefLifeSpanHandler
    {
        private ChromeWebBrowser webBrowser;
        public CwbLifeSpanHandler(ChromeWebBrowser browser)
        {
            webBrowser = browser;
        }

        protected override void OnAfterCreated(CefBrowser browser)
        {
            base.OnAfterCreated(browser);
            webBrowser.OnCreated(browser);
        }

        protected override bool DoClose(CefBrowser browser)
        {
            return false;
        }

        protected override bool OnBeforePopup(CefBrowser browser, CefFrame frame, string targetUrl, string targetFrameName, CefWindowOpenDisposition targetDisposition, bool userGesture, CefPopupFeatures popupFeatures, CefWindowInfo windowInfo, ref CefClient client, CefBrowserSettings settings, ref bool noJavascriptAccess)
        {
            bool res = false;
            if (!string.IsNullOrEmpty(targetUrl))
            {
                if (webBrowser.selfRequest != null)
                {
                    CefRequest req = CefRequest.Create();
                    req.FirstPartyForCookies = webBrowser.selfRequest.FirstPartyForCookies;
                    req.Options = webBrowser.selfRequest.Options;
                    System.Collections.Specialized.NameValueCollection h = new System.Collections.Specialized.NameValueCollection();
                    h.Add("Content-Type", "application/x-www-form-urlencoded");
                    req.Set(targetUrl, webBrowser.selfRequest.Method, null, webBrowser.selfRequest.GetHeaderMap());
                    webBrowser.selfRequest = req;
                }
                //在该处实现用当前页面打开所有新开的窗口
                webBrowser.OpenUrl(targetUrl);
                res = true;
                if (res)
                    return res;
            }
            return base.OnBeforePopup(browser, frame, targetUrl, targetFrameName, targetDisposition, userGesture, popupFeatures, windowInfo, ref client, settings, ref noJavascriptAccess);
            return res;
            
        }
    }

虽然修改了浏览器的新窗口处理方法,但浏览器并不会自动加载到我们的CwbCefLifeSpanHandler类,接下来我们需要在实现CefClient的ClientBrowser类中加上我们的CwbCefLifeSpanHandler实现类:


public sealed class ClientBrowser : CefClient
{
        private readonly CwbLifeSpanHandler _lifeSpanHandler;

        private ChromeWebBrowser webBrowser;
        public ClientBrowser(ChromeWebBrowser browser)
        {
            webBrowser = browser;
            _lifeSpanHandler = new CwbLifeSpanHandler(browser);
        }

        public override CefLifeSpanHandler GetLifeSpanHandler()
        {
            return _lifeSpanHandler;
        }
}


到此我们的工作就已经完成,可以直接运行程序进行测试了

版权声明:本文为博主原创文章,未经博主允许不得转载。

CEF Xilium.CefGlue 在当前窗口中打开所有链接(防止弹窗)

标签:cef xilium.cefglue在当   xilium.cefglue防止弹窗   

原文地址:http://blog.csdn.net/zhang116868/article/details/46945973

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!