快速上手
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>
参考链接:
快速开始
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
博客园博客