js和C#互相调用


快速上手

 

js和C#互相调用。

C#调用js比较容易。JS调用C#代码,现有两种方法。老方法的缺点是只支持单页,如果切换页面,原有创建的变量就失效了。新方法没有这些问题。

老方法:

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

CefSharp 与 js 相互调用

新方法

https://github.com/cefsharp/CefSharp/issues/2246

 

  var options = new CefSharp.BindingOptions() {CamelCaseJavascriptNames = false}; var bindingOptions = options; //For async object registration (equivalent to RegisterJsObject) this.Browser.JavascriptObjectRepository.Register("boundAsync", new BoundObject(), true, options); this.Browser.JavascriptObjectRepository.Register("boundAsync2", new AsyncBoundObject(), true, options);
复制代码
<script type="text/javascript">
    function aa() { // <embed user provided code here> CefSharp.BindObjectAsync("boundAsync", "bound").then(function (result) { boundAsync.hello('CefSharp'); }); }; function bb(){ CefSharp.BindObjectAsync("boundAsync2").then(function(result) { boundAsync2.hello('CefSharp'); }); } </script>
复制代码

 

demo1

 

参考链接:

 

快速开始

https://github.com/cefsharp/CefSharp/wiki/Quick-Start

wpf中使用

https://www.codeproject.com/Articles/881315/Display-HTML-in-WPF-and-CefSharp-Tutorial-Part

官方demo

https://github.com/cefsharp/CefSharp.MinimalExample

 博客园博客

https://www.cnblogs.com/M-Silencer/p/5846494.html

https://www.cnblogs.com/lonelyxmas/p/11397635.html


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM