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

版权声明: https://blog.csdn.net/qq_36130706/article/details/79705303

原文地址出: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);
        }
    }
}

				<script>
					(function(){
						function setArticleH(btnReadmore,posi){
							var winH = $(window).height();
							var articleBox = $("div.article_content");
							var artH = articleBox.height();
							if(artH > winH*posi){
								articleBox.css({
									'height':winH*posi+'px',
									'overflow':'hidden'
								})
								btnReadmore.click(function(){
									articleBox.removeAttr("style");
									$(this).parent().remove();
								})
							}else{
								btnReadmore.parent().remove();
							}
						}
						var btnReadmore = $("#btn-readmore");
						if(btnReadmore.length>0){
							if(currentUserName){
								setArticleH(btnReadmore,3);
							}else{
								setArticleH(btnReadmore,1.2);
							}
						}
					})()
				</script>
				</article>

没有更多推荐了,返回首页