C# JS互调 嵌入本地Html

这样的教程已经很多了,怕忘记,还是写出来。。。。。。

嵌入CEF就不用说了,直接主题

JS调用C#

string url = AppDomain.CurrentDomain.BaseDirectory + @"htmlindex.html";//debug文件夹本地Html路径
     ChromiumWebBrowser browser = new ChromiumWebBrowser(url);
    
     CefSharpSettings.LegacyJavascriptBindingEnabled = true;//切记设置这行代码
 
  //c#代码:
            BindingOptions bo = new BindingOptions();  //驼峰命名法
            bo.CamelCaseJavascriptNames =false;
            browser.RegisterAsyncJsObject("jsobj", new Test(), bo);
//Test 方法
    internal class Test
    {
        public Test(){}
        public void show()
        {
            MessageBox.Show("this is c#");
        }
    }

  

JS中调用C#方法 Html中

<!DOCTYPE html>
<html lang="zh-CN">
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
		<meta name="format-detection" content="telephone=no" />
		<script type="text/javascript" src="js/jquery1.9.1.min.js"></script>
	</head>
 
	<body>
    <button onclick="Get()">hi</button>
	</body>
</html>
<script type="text/javascript">
function Get()
{
  jsobj.show();//C#方法
}
</script>

  

C#调用js

browser.ExecuteScriptAsync("oce_one()");

  


原文地址:https://www.cnblogs.com/microtiger/p/13198323.html