cefsharp 与webbrowser简单对比概述

简介: 原文:cefsharp 与webbrowser简单对比概述 有个项目需要做个简单浏览器,从网上了解到几个相关的组件有winform自带的IE内核的WebBrowser,有第三方组件谷歌内核的webkit、cefsharp、chromiumfx等。
原文: cefsharp 与webbrowser简单对比概述

有个项目需要做个简单浏览器,从网上了解到几个相关的组件有winform自带的IE内核的WebBrowser,有第三方组件谷歌内核的webkit、cefsharp、chromiumfx等。

一开始是用的webbrowser 后来发现有些css样式是不兼容的于是又开始研究webkit这个有些css样式同样不能用,然后才开始研究cefsharp,从网上下载了57版本的但总是运行不起来后来发现装上vs中C++公共工具才行(注意即使装上vcredist_x86这个运行库也是不行),这个你不能要求人家客户装vs啊,后来也没找到到底是哪个动态库的问题。之后就下载了47版本。

按键事件

   不同于webbrowser通过事件PreviewKeyDown捕获键盘按键,cefsharp是通过实现接口IKeyboardHandler赋值委托KeyboardHandler来实现的

 web.KeyboardHandler = new CefKeyboardHandler(m_cell as CellControl);
 public class CefKeyboardHandler : IKeyboardHandler
    {
        private CellControl m_cell;

        /// <summary>
        /// 命名代理
        /// </summary>
        /// <param name="con"></param>
        /// <param name="text"></param>
        private delegate void PreKeyEventHandler(object sender,object webBrowser,int windowsKeyCode);

        public CefKeyboardHandler(CellControl cell)
        {
            m_cell = cell;
        }
        public bool OnKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey)
        {
            return true;
        }
        public bool OnPreKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey, ref bool isKeyboardShortcut)
        {
           m_cell.Invoke(new PreKeyEventHandler(PreKeyEvent),m_cell, browserControl, windowsKeyCode);
            return true;
        }

        private void PreKeyEvent(object sender, object webBrowser, int windowsKeyCode)
        {
            CellControl cell = sender as CellControl;
            cell.ControlPreviewKeyDown(webBrowser , new System.Windows.Forms.PreviewKeyDownEventArgs((Keys)windowsKeyCode));
            if (windowsKeyCode == (int)(Keys.Delete))
            {
                cell.Controls.Clear();
            }
           
        }



代理设置

   公司网络是需要代理才能上外网,webbrowser可以自动获取已经设置好的代理,cefsharp网上说可以通过setting.CefCommandLineArgs.Add("--proxy-server""http://127.0.0.1:8877");这个方式设置代理,可是不知道为啥我这边一直不行,后来发现一个通过实现接口IRequestHandler赋值委托RequestHandler来设置代理可行

  webBrowser.RequestHandler = new CefRequest();
 public class CefRequest : IRequestHandler
    {
        //
        // 摘要:
        //     Called when the browser needs credentials from the user.
        //
        // 参数:
        //   browserControl:
        //     The ChromiumWebBrowser control
        //
        //   browser:
        //     the browser object
        //
        //   frame:
        //     The frame object that needs credentials (This will contain the URL that is being
        //     requested.)
        //
        //   isProxy:
        //     indicates whether the host is a proxy server
        //
        //   host:
        //     hostname
        //
        //   port:
        //     port number
        //
        //   realm:
        //     realm
        //
        //   scheme:
        //     scheme
        //
        //   callback:
        //     Callback interface used for asynchronous continuation of authentication requests.
        //
        // 返回结果:
        //     Return true to continue the request and call CefAuthCallback::Continue() when
        //     the authentication information is available. Return false to cancel the request.
        public bool GetAuthCredentials(IWebBrowser browserControl, IBrowser browser, IFrame frame, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback)
        {
            if(isProxy)
            {
                if (string.IsNullOrWhiteSpace(ProxyThornInfo.Name))
                {
                    ProxyThornInfo.InitInfo(ProxyThornInfo.DefaultFile);
                }
            }
            //callback.Cancel();
            callback.Continue(ProxyThornInfo.Name, ProxyThornInfo.PassWord);
            return true;
        }

        //
        // 摘要:
        //     Called before browser navigation. If the navigation is allowed CefSharp.IWebBrowser.FrameLoadStart
        //     and CefSharp.IWebBrowser.FrameLoadEnd will be called. If the navigation is canceled
        //     CefSharp.IWebBrowser.LoadError will be called with an ErrorCode value of CefSharp.CefErrorCode.Aborted.
        //
        // 参数:
        //   browserControl:
        //     the ChromiumWebBrowser control
        //
        //   browser:
        //     the browser object
        //
        //   frame:
        //     The frame the request is coming from
        //
        //   request:
        //     the request object - cannot be modified in this callback
        //
        //   isRedirect:
        //     has the request been redirected
        //
        // 返回结果:
        //     Return true to cancel the navigation or false to allow the navigation to proceed.
        public bool OnBeforeBrowse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, bool isRedirect)
        {
            return false;
        }
        //
        // 摘要:
        //     Called before a resource request is loaded. For async processing return CefSharp.CefReturnValue.ContinueAsync
        //     and execute CefSharp.IRequestCallback.Continue(System.Boolean) or CefSharp.IRequestCallback.Cancel
        //
        // 参数:
        //   browserControl:
        //     The ChromiumWebBrowser control
        //
        //   browser:
        //     the browser object
        //
        //   frame:
        //     The frame object
        //
        //   request:
        //     the request object - can be modified in this callback.
        //
        //   callback:
        //     Callback interface used for asynchronous continuation of url requests.
        //
        // 返回结果:
        //     To cancel loading of the resource return CefSharp.CefReturnValue.Cancel or CefSharp.CefReturnValue.Continue
        //     to allow the resource to load normally. For async return CefSharp.CefReturnValue.ContinueAsync
        public CefReturnValue OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
        {
            return CefReturnValue.Continue;
        }

        //
        // 摘要:
        //     Called on the CEF IO thread when a resource response is received. To allow the
        //     resource to load normally return false. To redirect or retry the resource modify
        //     request (url, headers or post body) and return true. The response object cannot
        //     be modified in this callback.
        //
        // 参数:
        //   browserControl:
        //     The ChromiumWebBrowser control
        //
        //   browser:
        //     the browser object
        //
        //   frame:
        //     The frame that is being redirected.
        //
        //   request:
        //     the request object
        //
        //   response:
        //     the response object - cannot be modified in this callback
        //
        // 返回结果:
        //     To allow the resource to load normally return false. To redirect or retry the
        //     resource modify request (url, headers or post body) and return true.
        public bool OnResourceResponse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response)
        {
            return false;
        }

        //
        // 摘要:
        //     Called to handle requests for URLs with an invalid SSL certificate. Return true
        //     and call CefSharp.IRequestCallback.Continue(System.Boolean) either in this method
        //     or at a later time to continue or cancel the request. If CefSettings.IgnoreCertificateErrors
        //     is set all invalid certificates will be accepted without calling this method.
        //
        // 参数:
        //   browserControl:
        //     the ChromiumWebBrowser control
        //
        //   browser:
        //     the browser object
        //
        //   errorCode:
        //     the error code for this invalid certificate
        //
        //   requestUrl:
        //     the url of the request for the invalid certificate
        //
        //   sslInfo:
        //     ssl certificate information
        //
        //   callback:
        //     Callback interface used for asynchronous continuation of url requests. If empty
        //     the error cannot be recovered from and the request will be canceled automatically.
        //
        // 返回结果:
        //     Return false to cancel the request immediately. Return true and use CefSharp.IRequestCallback
        //     to execute in an async fashion.
        public bool OnCertificateError(IWebBrowser browserControl, IBrowser browser, CefErrorCode errorCode, string requestUrl, ISslInfo sslInfo, IRequestCallback callback)
        {
            return true;
        }

        //
        // 摘要:
        //     Called on the UI thread before OnBeforeBrowse in certain limited cases where
        //     navigating a new or different browser might be desirable. This includes user-initiated
        //     navigation that might open in a special way (e.g. links clicked via middle-click
        //     or ctrl + left-click) and certain types of cross-origin navigation initiated
        //     from the renderer process (e.g. navigating the top-level frame to/from a file
        //     URL).
        //
        // 参数:
        //   browserControl:
        //     the ChromiumWebBrowser control
        //
        //   browser:
        //     the browser object
        //
        //   frame:
        //     The frame object
        //
        //   targetUrl:
        //     target url
        //
        //   targetDisposition:
        //     The value indicates where the user intended to navigate the browser based on
        //     standard Chromium behaviors (e.g. current tab, new tab, etc).
        //
        //   userGesture:
        //     The value will be true if the browser navigated via explicit user gesture (e.g.
        //     clicking a link) or false if it navigated automatically (e.g. via the DomContentLoaded
        //     event).
        //
        // 返回结果:
        //     Return true to cancel the navigation or false to allow the navigation to proceed
        //     in the source browser's top-level frame.
        public bool OnOpenUrlFromTab(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, WindowOpenDisposition targetDisposition, bool userGesture)
        {
            return false;
        }

        //
        // 摘要:
        //     Called when a plugin has crashed
        //
        // 参数:
        //   browserControl:
        //     the ChromiumWebBrowser control
        //
        //   browser:
        //     the browser object
        //
        //   pluginPath:
        //     path of the plugin that crashed
        public void OnPluginCrashed(IWebBrowser browserControl, IBrowser browser, string pluginPath)
        {

        }

        //
        // 摘要:
        //     Called on the UI thread to handle requests for URLs with an unknown protocol
        //     component. SECURITY WARNING: YOU SHOULD USE THIS METHOD TO ENFORCE RESTRICTIONS
        //     BASED ON SCHEME, HOST OR OTHER URL ANALYSIS BEFORE ALLOWING OS EXECUTION.
        //
        // 参数:
        //   browserControl:
        //     The ChromiumWebBrowser control
        //
        //   browser:
        //     the browser object
        //
        //   url:
        //     the request url
        //
        // 返回结果:
        //     return to true to attempt execution via the registered OS protocol handler, if
        //     any. Otherwise return false.
        public bool OnProtocolExecution(IWebBrowser browserControl, IBrowser browser, string url)
        {
            return true;
        }
        //
        // 摘要:
        //     Called when JavaScript requests a specific storage quota size via the webkitStorageInfo.requestQuota
        //     function. For async processing return true and execute CefSharp.IRequestCallback.Continue(System.Boolean)
        //     at a later time to grant or deny the request or CefSharp.IRequestCallback.Cancel
        //     to cancel.
        //
        // 参数:
        //   browserControl:
        //     The ChromiumWebBrowser control
        //
        //   browser:
        //     the browser object
        //
        //   originUrl:
        //     the origin of the page making the request
        //
        //   newSize:
        //     is the requested quota size in bytes
        //
        //   callback:
        //     Callback interface used for asynchronous continuation of url requests.
        //
        // 返回结果:
        //     Return false to cancel the request immediately. Return true to continue the request
        //     and call CefSharp.IRequestCallback.Continue(System.Boolean) either in this method
        //     or at a later time to grant or deny the request.
        public bool OnQuotaRequest(IWebBrowser browserControl, IBrowser browser, string originUrl, long newSize, IRequestCallback callback)
        {
            return true;
        }
        //
        // 摘要:
        //     Called when the render process terminates unexpectedly.
        //
        // 参数:
        //   browserControl:
        //     The ChromiumWebBrowser control
        //
        //   browser:
        //     the browser object
        //
        //   status:
        //     indicates how the process terminated.
        public void OnRenderProcessTerminated(IWebBrowser browserControl, IBrowser browser, CefTerminationStatus status)
        {

        }
        //
        // 摘要:
        //     Called on the browser process UI thread when the render view associated with
        //     browser is ready to receive/handle IPC messages in the render process.
        //
        // 参数:
        //   browserControl:
        //     The ChromiumWebBrowser control
        //
        //   browser:
        //     the browser object
        public void OnRenderViewReady(IWebBrowser browserControl, IBrowser browser)
        {

        }
        //
        // 摘要:
        //     Called on the CEF IO thread when a resource load has completed.
        //
        // 参数:
        //   browserControl:
        //     The ChromiumWebBrowser control
        //
        //   browser:
        //     the browser object
        //
        //   frame:
        //     The frame that is being redirected.
        //
        //   request:
        //     the request object - cannot be modified in this callback
        //
        //   response:
        //     the response object - cannot be modified in this callback
        //
        //   status:
        //     indicates the load completion status
        //
        //   receivedContentLength:
        //     is the number of response bytes actually read.
        public void OnResourceLoadComplete(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength)
        {

        }
        //
        // 摘要:
        //     Called on the IO thread when a resource load is redirected. The CefSharp.IRequest.Url
        //     parameter will contain the old URL and other request-related information.
        //
        // 参数:
        //   browserControl:
        //     The ChromiumWebBrowser control
        //
        //   browser:
        //     the browser object
        //
        //   frame:
        //     The frame that is being redirected.
        //
        //   request:
        //     the request object - cannot be modified in this callback
        //
        //   newUrl:
        //     the new URL and can be changed if desired
        public void OnResourceRedirect(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, ref string newUrl)
        {

        }
其他的ChromiumWebBrowser.Handler同上

JavaScript互调

    在webbrowser中通过设置  

[System.Runtime.InteropServices.ComVisible(true)]

public class JavaScripAction

webBrowser1.ObjectForScripting = new JavaScripAction();
就可在javascrip中通过window.external来调用C#类JavaScriptAction中的方法,在C#中通过来调用javascrip中的函数
web.RegisterJsObject("objbine", java, false);这里的objbine是我们自己起的这个注册的名字实体,注意不能和java中特殊含义的文字重复例如external否则没有效果

 
 

webBrowser1.Document.InvokeScript("Messageaa", objects);

在cefsharp中是通过注册来实现java与C#类的绑定的

JavaScripAction java = new JavaScripAction(dataSvc, bbShow, pdpcShowBb);

这样在javascrip中可以通过

window.objbine.InvokeFunc('0','1','cunction','1,2,3');

这种方式来调用C#类JavaScriptaction中的方法InvokFunc方法了,需要注意的是如果

在C#中调用JavaScript中的函数如下

web.ExecuteScriptAsync(string methodname,params object[] args)

需要注意的是如果JavaScript中的函数名是没有参数的这里的methodname需要在函数名后面加上括号的如果没有参数就不能加括号。







目录
相关文章
|
JavaScript 前端开发 C#
WPF技术之WebBrowser控件
WPF WebBrowser控件用于在WPF应用程序中嵌入浏览器功能。
302 0
|
C#
C#webBrowser使用代理服务器的方法winform
其实在C#中使用webBrowser大家应该都会了,论坛也有很多相前的例子大家可以查询一下就知道了但是像直接使用浏览器一样设置代理 的方法可能很多人还不知道吧。这个其实是调用一个Dll文件进行设置的,下面大家跟我一起来看看吧首先还是要先建一个结构就是代理信息的结构体如下 [C#] 纯文本查看 复制...
1465 0
WinForm使用CefSharp,嵌入浏览器
WinForm使用CefSharp,嵌入浏览器
563 0
|
Web App开发 JavaScript C#
在WPF中使用CefSharp嵌入浏览器
原文:在WPF中使用CefSharp嵌入浏览器 日常开发中,我们需要将一些Web页面嵌入到桌面客户端软件中。下面我们使用CefSharp嵌入浏览器来实现。  首先先介绍一下CefSharp嵌入式浏览器,它是基于Google浏览器的一个组件,我们可以在WPF/WinForm客户端软件中使用它。
2471 0
|
C#
CefSharp For WPF基本使用
原文:CefSharp For WPF基本使用 Nuget引用 CefSharp.Wpf CefSharp.Common cef.redist.x64 cef.redist.x86 直接搜索安装CefSharp.
4382 0
|
JavaScript C#
wpf CefSharp 与 js交互
原文:wpf CefSharp 与 js交互 通过 NuGet 获取 CefSharp.WpF 组件。  xmlns:cefSharp="clr-namespace:CefSharp.
3928 0
|
前端开发 JavaScript C#
WPF中使用cefsharp
原文:WPF中使用cefsharp 新入职一家公司,由写服务端接口变成了软硬件通信、服务器、客户端、C/S、B/S乱七八糟各种打杂。
2043 0
|
Android开发
CefSharp 在同一窗口打开链接的方法
原文:CefSharp 在同一窗口打开链接的方法 摘要 在winform中使用cefsharp的时候,我们在浏览网页的时候,想在同一个窗口打开链接,而不是创建新的窗口。可以通过下面的方法实现。 解决方案 CefSharp 中控制弹窗的接口是 ILifeSpanHandler ,可以对 OnBeforePopup 进行重写,即可控制是否弹出新窗体。
2362 0
|
C#
C#:在AnyCPU模式下使用CefSharp
    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------       本篇博客讲述如何在AnyCPU模式下使用CefSharp 因为在某些情况下,不得不用AnyCPU,但是CefSharp支持的是86和64位俩种模式,所以在我查阅了很多国内外的资料下,总结出来的一些精华   参考地址: https://ourcodeworld.
7575 0