Cefsharp js调用c#与c#调用js

原文地址出:https://github.com/cefsharp/CefSharp/issues/2246

在cefsharp63.02版本下注册绑定事件需要在绑定事件之前申明这段代码。否则不能绑定、报错;

CefSharpSettings.LegacyJavascriptBindingEnabled = true

工程文件与文章(https://www.2cto.com/kf/201801/713008.html)相似;

由于本人还在研究,不胜求解;又因才疏学浅,不足之处,还望前辈们指出;

附源码如下:

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;


namespace cefSharp
{
    public partial class Form1 : Form
    {
        ChromiumWebBrowser browser;
        public Form1()
        {
            InitializeComponent();
            browser = new ChromiumWebBrowser(@"C:\Users\Administrator\Desktop\cefSharp\cefSharp\test.html");
            browser.Dock = DockStyle.Fill;
            panel1.Controls.Add(browser);
            CefSharpSettings.LegacyJavascriptBindingEnabled = true;//新cefsharp绑定需要优先申明
            browser.RegisterJsObject("boud", new JsEvent(),new CefSharp.BindingOptions() { CamelCaseJavascriptNames = false } );
        }


        private void button1_Click(object sender, EventArgs e)
        {
            browser.ExecuteScriptAsync("oce_one()");
        }
    }
    public class JsEvent
    {
        public string MessageText { get; set; }
        public void ShowTest()
        {
            MessageBox.Show("this in c#.\n\r" + MessageText);


        }
        public void ShowTestArg(string ss)
        {
            MessageBox.Show("收到Js参数的调用\n\r"+ss);
        }
    }
}

你可能感兴趣的:(CefSharp)