0


【C#】用CefSharp.ChromiumWebBrowser做winform开发过程中遇到的一些坑

文章目录

使用键盘F5刷新、F12打开控制台

publicclassCEFKeyBoardHander:IKeyboardHandler{publicboolOnKeyEvent(IWebBrowser browserControl,IBrowser browser,KeyType type,int windowsKeyCode,int nativeKeyCode,CefEventFlags modifiers,bool isSystemKey){if(type == KeyType.KeyUp && Enum.IsDefined(typeof(Keys), windowsKeyCode)){var key =(Keys)windowsKeyCode;switch(key){case Keys.F12:
                    browser.ShowDevTools();break;case Keys.F5:if(modifiers == CefEventFlags.ControlDown){//MessageBox.Show("ctrl+f5");
                        browser.Reload(true);//强制忽略缓存}else{//MessageBox.Show("f5");
                        browser.Reload();}break;}}returnfalse;}publicboolOnPreKeyEvent(IWebBrowser browserControl,IBrowser browser,KeyType type,int windowsKeyCode,int nativeKeyCode,CefEventFlags modifiers,bool isSystemKey,refbool isKeyboardShortcut){returnfalse;}}
WebBrowser =newChromiumWebBrowser();this.Controls.Add(WebBrowser);
WebBrowser.KeyboardHandler =newCEFKeyBoardHander();

浏览器默认下载弹窗

publicclassCefDownloadHandler:IDownloadHandler{publiceventEventHandler<DownloadItem> OnBeforeDownloadFired;publiceventEventHandler<DownloadItem> OnDownloadUpdatedFired;publicboolCanDownload(IWebBrowser chromiumWebBrowser,IBrowser browser,string url,string requestMethod){//可以支持全部,也可以根据配置去自定义支持的文件类型//return "xls|xlsx".Split('|').ToList().Contains(url.Substring(url.LastIndexOf(".") + 1).ToLower());returntrue;}publicvoidOnBeforeDownload(IWebBrowser chromiumWebBrowser,IBrowser browser,DownloadItem downloadItem,IBeforeDownloadCallback callback){
        OnBeforeDownloadFired?.Invoke(this, downloadItem);if(!callback.IsDisposed){using(callback){
                callback.Continue(downloadItem.SuggestedFileName,showDialog:true);}}}publicvoidOnDownloadUpdated(IWebBrowser chromiumWebBrowser,IBrowser browser,DownloadItem downloadItem,IDownloadItemCallback callback){
        OnDownloadUpdatedFired?.Invoke(this, downloadItem);}}
WebBrowser =newChromiumWebBrowser();
WebBrowser.DownloadHandler =newCefDownloadHandler();

禁用GPU配置

var setting =newCefSettings();
setting.CefCommandLineArgs.Add("disable-gpu","1")

跨域问题

setting.CefCommandLineArgs.Add("--disable-web-security","");

待续未完……


本文转载自: https://blog.csdn.net/u010726809/article/details/135502253
版权归原作者 饺子大魔王12138 所有, 如有侵权,请联系我们删除。

“【C#】用CefSharp.ChromiumWebBrowser做winform开发过程中遇到的一些坑”的评论:

还没有评论