您现在的位置是:首页 > 唯美句子

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

作者:亙句时间:2024-04-24 11:45:24分类:唯美句子

简介  文章浏览阅读812次,点赞6次,收藏9次。【代码】【C#】用CefSharp.ChromiumWebBrowser做winform开发过程中遇到的一些坑。_c# chromiumwebbrowser

点击全文阅读

文章目录

使用键盘F5刷新、F12打开控制台浏览器默认下载弹窗禁用GPU配置跨域问题

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

public class CEFKeyBoardHander : IKeyboardHandler{    public bool OnKeyEvent(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;            }        }        return false;    }    public bool OnPreKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey, ref bool isKeyboardShortcut)    {        return false;    }}
WebBrowser = new ChromiumWebBrowser();this.Controls.Add(WebBrowser);WebBrowser.KeyboardHandler = new CEFKeyBoardHander();

浏览器默认下载弹窗

public class CefDownloadHandler : IDownloadHandler{    public event EventHandler<DownloadItem> OnBeforeDownloadFired;    public event EventHandler<DownloadItem> OnDownloadUpdatedFired;    public bool CanDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, string url, string requestMethod)    {        //可以支持全部,也可以根据配置去自定义支持的文件类型        //return "xls|xlsx".Split('|').ToList().Contains(url.Substring(url.LastIndexOf(".") + 1).ToLower());        return true;    }    public void OnBeforeDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback)    {        OnBeforeDownloadFired?.Invoke(this, downloadItem);        if (!callback.IsDisposed)        {            using (callback)            {                callback.Continue(downloadItem.SuggestedFileName, showDialog: true);            }        }    }    public void OnDownloadUpdated(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback)    {        OnDownloadUpdatedFired?.Invoke(this, downloadItem);    }}
WebBrowser = new ChromiumWebBrowser();WebBrowser.DownloadHandler = new CefDownloadHandler();

禁用GPU配置

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

跨域问题

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

待续未完……

点击全文阅读

郑重声明:

本站所有活动均为互联网所得,如有侵权请联系本站删除处理

我来说两句