1. 程式人生 > >Cefsharp js呼叫c#與c#呼叫js

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