【C#】用CefSharp.ChromiumWebBrowser做winform开发过程中遇到的一些坑
饺子大魔王12138 2024-07-17 17:03:02 阅读 86
文章目录
使用键盘F5刷新、F12打开控制台浏览器默认下载弹窗禁用GPU配置跨域问题
使用键盘F5刷新、F12打开控制台
<code>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", "");
待续未完……
上一篇: 作为前端 leader,怎么快速搭建多环境CICD自动化部署?
本文标签
声明
本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。