日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C#?cefSharep控件的使用詳情_C#教程

作者:姚家灣 ? 更新時間: 2023-03-26 編程語言

C# 有一個WebBrowse 控件,但是它是基于IE 的,對HTML5 不友好,為了能夠完美地支持HTML5 ,需要使用Google的嵌入式Chrome引擎。cefsharp 包分裝了Chome 引擎。下面的程序小試了一下。遇到的問題是如何訪問本地文檔。網絡上介紹的方法有點過時了。

要能夠訪問本地文件需要下面的代碼

// 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這兩個屬性了。網站上介紹使用命令 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 的屬性中添加了這兩個開關,突然就好了,而且去掉了上面的兩條語句也可以了。不知道為什么。下面是可以運行的代碼。供需要的的人參考

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);
        }
 
      
    }
}

原文鏈接:https://blog.csdn.net/yaojiawan/article/details/128583403

欄目分類
最近更新