C# CefSharp 新版本(83 以后版本) 如何在js中直接调用c#类

最近发现升级 CefSharp 到最新版本(86.0.241)后无法使用JS直接调用C#类了

以前使用的83.4.20版本,直接使用Register注册,就可以在JS中使用注册名 "callbackObj" 直接调用到注册的C#类

具体代码:

  private readonly ChromiumWebBrowser _browser;
 
  public MainForm()
        {
            InitializeComponent();
            _browser = new ChromiumWebBrowser(URL)
            {
                Dock = DockStyle.Fill,
            };
            this.Controls.Add(browser);
 
            CallbackObjectForJs callbackObjectForJs = new CallbackObjectForJs();
            
            CefSharpSettings.LegacyJavascriptBindingEnabled = true;
            CefSharpSettings.WcfEnabled = true;
            //注册
            _browser.JavascriptObjectRepository.Register("callbackObj", callbackObjectForJs, isAsync: false, options: BindingOptions.DefaultBinder);
           
            // 按F12显示调试工具
            _browser.KeyboardHandler = new CEFKeyBoardHander();
        }
 
    /// <summary>
    /// JS回调类
    /// </summary>
    [ComVisible(true)]
    public class CallbackObjectForJs
    {
        public void Test()
        {
            Thread th = new Thread(() =>
            {
                MessageBox.Show("Test");
            });
 
            th.SetApartmentState(ApartmentState.STA);
            th.Start();
        }
    }

  JS代码

<!DOCTYPE html>
 
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    hello word!!!
    <button id="btnTest" onclick="btnTest()">测试</button>
    <script type="text/javascript">
        function btnTest() {
            callbackObj.test(); //注意 C#中的类的方法开头字母会变小写
        }
    </script>
</body>
</html>

  84以后的版本注册后想要在js中使用callbackObjectForJs类中的方法,需要先在js中调用

CefSharp.BindObjectAsync('callbackObj');

  具体代码:

<!DOCTYPE html>
 
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    hello word!!!
    <button id="btnTest" onclick="btnTest()">测试</button>
    <script type="text/javascript">
        function btnTest() {
            CefSharp.BindObjectAsync('callbackObj');
            callbackObj.test(); //注意 C#中的类的方法开头字母会变小写
        }
    </script>
</body>
</html>

  

为了不在每个html界面都写bind,可以把BindObjectAsync 写到browser的加载完成事件FrameLoadEnd事件中,

具体代码:

public MainForm()
        {
            InitializeComponent();
            _browser = new ChromiumWebBrowser(URL)
            {
                Dock = DockStyle.Fill,
            };
            this.Controls.Add(browser);
 
            CallbackObjectForJs callbackObjectForJs = new CallbackObjectForJs();
            
            CefSharpSettings.LegacyJavascriptBindingEnabled = true;
            CefSharpSettings.WcfEnabled = true;
            //注册
            _browser.JavascriptObjectRepository.Register("callbackObj", callbackObjectForJs, isAsync: false, options: BindingOptions.DefaultBinder);
           
            //加载完成事件
            browser.FrameLoadEnd += (t, s) =>
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("(function(){")
                    .Append("CefSharp.BindObjectAsync('callbackObj');")
                    .Append("})();");
                browser.GetFocusedFrame().EvaluateScriptAsync(sb.ToString());
            };
            // 按F12显示调试工具
            _browser.KeyboardHandler = new CEFKeyBoardHander();
        }

  这样就可以在JS中继续使用callbackObj回调了

 

注意:经测试仍然不行,继续使用 83.4.20 版本了

 

原文:https://blog.csdn.net/txg1369540047/article/details/110645737

posted @ 2021-12-01 16:09  一万年以前  阅读(382)  评论(0编辑  收藏  举报