C# cefSharep控件的使用詳情
C# 有一個(gè)WebBrowse 控件,但是它是基于IE 的,對(duì)HTML5 不友好,為了能夠完美地支持HTML5 ,需要使用Google的嵌入式Chrome引擎。cefsharp 包分裝了Chome 引擎。下面的程序小試了一下。遇到的問題是如何訪問本地文檔。網(wǎng)絡(luò)上介紹的方法有點(diǎn)過時(shí)了。
要能夠訪問本地文件需要下面的代碼
// 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這兩個(gè)屬性了。網(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 的屬性中添加了這兩個(gè)開關(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
c#項(xiàng)目實(shí)現(xiàn)發(fā)布到服務(wù)器全過程
這篇文章主要介紹了c#項(xiàng)目實(shí)現(xiàn)發(fā)布到服務(wù)器全過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04

