其他分享
首页 > 其他分享> > js 调用 ChromiumWebBrowser 方法

js 调用 ChromiumWebBrowser 方法

作者:互联网

 C# 代码

public partial class FormCef : Form
{
    public ChromiumWebBrowser chrom = new ChromiumWebBrowser();
    public FormCef()
        {
            InitializeComponent();
 
            if (!Cef.IsInitialized)
            {
                CefSettings settings = new CefSettings();
 
                //设置JS跨域;
                settings.MultiThreadedMessageLoop = true;
                settings.CefCommandLineArgs.Add("--disable-web-security", "");
 
                Cef.Initialize(settings, performDependencyCheck: false, browserProcessHandler: null);
 
            }
            CefSharpSettings.WcfEnabled = true;
            //老版本是:CefSharpSettings.LegacyJavascriptBindingEnabled = true;
            chrom.JavascriptObjectRepository.Settings.LegacyBindingEnabled = true;
            //注册方法
            chrom.JavascriptObjectRepository.Register("winformFun", new winformFun(), isAsync: false, options: BindingOptions.DefaultBinder);
            
        }
}
 
//测试方法
public class winformFun
    {
        public void showTest2()
        {
            MessageBox.Show("ShowTest2222");
        }
 
        public void showTest(string txt)
        {
            MessageBox.Show("this in C#.\n\r" + txt);
        }
    }

 

JS代码:

<html>
 
<input type="button" onclick="ShowTest()" value="winformFun.ShowTest" />
<script language=javascript>
 
function ShowTest(){
     winformFun.showTest("hello");
 }
 
</script>
</html>
 

 

标签:调用,settings,ChromiumWebBrowser,js,new,winformFun,true,public
来源: https://www.cnblogs.com/hanjun0612/p/16499290.html