C# cefSharep控件的使用詳情
C# 有一個WebBrowse 控件,但是它是基于IE 的,對HTML5 不友好,為了能夠完美地支持HTML5 ,需要使用Google的嵌入式Chrome引擎。cefsharp 包分裝了Chome 引擎。下面的程序小試了一下。遇到的問題是如何訪問本地文檔。網(wǎng)絡(luò)上介紹的方法有點過時了。
要能夠訪問本地文件需要下面的代碼
// Allow the use of local resources in the browser ? ? BrowserSettings browserSettings = new BrowserSettings(); ? ? browserSettings.FileAccessFromFileUrls = CefState.Enabled; ? ? browserSettings.UniversalAccessFromFileUrls = CefState.Enabled; ? ? chromeBrowser.BrowserSettings = browserSettings;
但是新的cefsharp 的版本不支持FileAccessFromFileUrls和UniversalAccessFromFileUrls這兩個屬性了。網(wǎng)站上介紹使用命令 flag (--allow-universal-access-from-files)和(allow-file-access-from-files)。我嘗試添加
settings.CefCommandLineArgs.Add("allow-universal-access-from-files","1"); settings.CefCommandLineArgs.Add("allow-file-access-from-files","1");
又在電腦中的Chrome 的屬性中添加了這兩個開關(guān),突然就好了,而且去掉了上面的兩條語句也可以了。不知道為什么。下面是可以運(yùn)行的代碼。供需要的的人參考
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using CefSharp; using CefSharp.WinForms; using System.IO; using System.Diagnostics; namespace WebkitTest { public partial class Form1 : Form { ChromiumWebBrowser CWebBrowser; public void InitializeChromium() { CefSettings settings = new CefSettings(); // settings.CefCommandLineArgs.Add("allow-universal-access-from-files","1"); // settings.CefCommandLineArgs.Add("allow-file-access-from-files","1"); Cef.Initialize(settings); // Create a browser component CWebBrowser = new ChromiumWebBrowser("File://E:/yao2022/HMI/views/index.html"); // Add it to the form and fill it to the form window. splitContainer1.Panel1.Controls.Add(CWebBrowser); CWebBrowser.Dock = DockStyle.Fill; // CWebBrowser.Load("File://E:/yao2022/HMI/views/index.html"); } public Form1() { InitializeComponent(); InitializeChromium(); } private void btn_Browse_Click(object sender, EventArgs e) { CWebBrowser.Load("www.baidu.com"); } private void btn_load_Click(object sender, EventArgs e) { CWebBrowser.Load(textBox1.Text); } } }
到此這篇關(guān)于C# cefSharep控件的使用的文章就介紹到這了,更多相關(guān)C# cefSharep內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#并發(fā)編程之a(chǎn)sync和await關(guān)鍵字詳解
對于?async?和?await?兩個關(guān)鍵字,對于一線開發(fā)人員再熟悉不過了,到處都是它們的身影,下面小編就來和大家記錄匯總下它們的使用吧2023-07-07C#利用Label標(biāo)簽控件模擬窗體標(biāo)題的移動及窗體顏色不斷變換效果
Label標(biāo)簽控件相信對大家來說都不陌生,下面這篇文章主要給大家介紹了關(guān)于C#利用Label標(biāo)簽控件模擬窗體標(biāo)題的移動及窗體顏色不斷變換效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-12-12C#筆記之EF Code First 數(shù)據(jù)模型 數(shù)據(jù)遷移
EF 中 Code First 的數(shù)據(jù)遷移網(wǎng)上有很多資料,我這份并沒什么特別。Code First 創(chuàng)建視圖網(wǎng)上也有很多資料,但好像很麻煩,而且親測好像是無效的方法(可能是我太笨,沒搞成功),我摸索出了一種簡單有效的方法,這里分享給大家2021-09-09