基于 CefSharp v91.1.23

入门Demo

1. 可以使用NuGet包或者从Github获取CefSharp
2. 添加引用

using CefSharp;
using CefSharp.WinForms;

3. Form上添加一个panel。
4. 初始化浏览器

public Form1()
{
    InitializeComponent();
    var chromeBrowser = new ChromiumWebBrowser("https://www.baidu.com");
    panel1.Controls.Add(chromeBrowser);
}

入门demo2

public partial class Form1 : Form
{
    public ChromiumWebBrowser chromeBrowser;
    public Form1()
    {
        InitializeComponent();
        InitializeChromium();
    }
    public void InitializeChromium()
    {
        CefSettings settings = new CefSettings();
        Cef.Initialize(settings);
        chromeBrowser = new ChromiumWebBrowser("https://www.baidu.com");
        panel1.Controls.Add(chromeBrowser);
        chromeBrowser.Dock = DockStyle.Fill;
    }     
}

JS调用WinForm方法

JavaScript Binding API 说明。

RegisterJsObject

// 旧方法
browser.RegisterJsObject("bound", new BoundObject(), options: BindingOptions.DefaultBinder);

// 替换为
CefSharpSettings.WcfEnabled = true;
browser.JavascriptObjectRepository.Settings.LegacyBindingEnabled = true;
browser.JavascriptObjectRepository.Register("bound", new BoundObject(), isAsync:false, options: BindingOptions.DefaultBinder);

RegisterAsyncJsObject

// 旧方法
browser.RegisterAsyncJsObject("boundAsync", new AsyncBoundObject(), options: BindingOptions.DefaultBinder);

// 替换为
browser.JavascriptObjectRepository.Settings.LegacyBindingEnabled = true;
browser.JavascriptObjectRepository.Register("boundAsync", new AsyncBoundObject(), isAsync: true, options: BindingOptions.DefaultBinder);

C#

public void InitializeChromium()
{
    string path = AppDomain.CurrentDomain.BaseDirectory + "index.html";
    browser = new ChromiumWebBrowser(path)
    {
        Dock = DockStyle.Fill,
    };
    panel1.Controls.Add(browser);

    CefSharpSettings.WcfEnabled = true;
    browser.JavascriptObjectRepository.Settings.LegacyBindingEnabled = true;
    browser.JavascriptObjectRepository.Register("bound", new BoundObject(), isAsync: false, options: BindingOptions.DefaultBinder);
    browser.JavascriptObjectRepository.Register("boundAsync", new AsyncBoundObject(), isAsync: true, options: BindingOptions.DefaultBinder);
}
public class BoundObject
{
    public int Add(int a, int b)
    {
        DateTime dt1 = DateTime.Now;
        while ((DateTime.Now - dt1).TotalMilliseconds < 3000) Application.DoEvents();
        return a + b;
    }
}
public class AsyncBoundObject
{
    public int Add(int a, int b)
    {
        DateTime dt1 = DateTime.Now;
        while ((DateTime.Now - dt1).TotalMilliseconds < 3000) Application.DoEvents();
        return a + b;
    }
}

js

<!DOCTYPE html>
<head>
    <meta charset="utf-8" />
    <title>测试</title>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
</head>
<body>
    <button id="test">弹窗</button>
    <button id="test1">同步</button>
    <button id="test2">异步</button>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#test").click(function () {
                alert("立刻弹窗");
            });
            $("#test1").click(function () {
                CefSharp.BindObjectAsync("bound");
                var res = bound.add(16, 2);
                alert(res);
            });
            $("#test2").click(async function () {
                await CefSharp.BindObjectAsync("boundAsync");
                boundAsync.add(16, 2).then(function (res) {
                    alert(res);
                });
            });
        });
    </script>
</body>
</html>

WinForm调用JS方法

browser.ExecuteScriptAsync("test(\"dbmj\")");
<script type="text/javascript">
    function test(a){
        alert("C#调用JS方法"+a);
    }
</script>

C#和JS互调完整代码

该部分仅登录用户可见


加载页面

把输入网址更改为输入页面内容
方式一:

const string strHTML = "<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title>标题</title></head><body><h1>标题</h1></body></html>";
browser = new ChromiumWebBrowser("data:text/html;base64," + Convert.ToBase64String(Encoding.UTF8.GetBytes(strHTML)));

方式二:

const string strHTML = "<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title>标题</title></head><body><h1>标题</h1></body></html>";
browser.LoadHtml(strHTML, true);
最后修改:2021 年 07 月 28 日
男宾一位~ 欢迎下次再来!