加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

CefSharp与JS相互调用注意事项

(2018-12-01 10:13:45)
标签:

cefsharp

js

分类: .NET


1、C#调用js


    private void btnAlert_Click(object sender, EventArgs e)
    {
        this.browser.GetBrowser().MainFrame.Execute;
        //txtAccount
        this.browser.GetBrowser().MainFrame.Execute;
    }

 

2、js调用C#

首要要创建被调用的C#对象和方法,注意方法名要小写,大写的时候,会调用失败!

    public class CefAsyncJS
    {
        private string LoginAccount = ConfigurationManager.AppSettings["LoginAccount"];


        public string getLoginAccount()
        {
            return LoginAccount;
        }
        public void setLoginAccount(string data)
        {
            LoginAccount = data ?? "";
        }

}

 

然后注册这个类

//注册C#对象,用来在js里面调用

//注意:RegisterAsyncJsObject的name必须是小写字母开头

//注意:CefAsyncJS类被JS调用的函数也必须是小写字母开头

CefSharpSettings.LegacyJavascriptBindingEnabled = true;
chromeBrowser.RegisterJsObject("cefAsyncJS", new CefAsyncJS(), BindingOptions.DefaultBinder);

 

这样,就能直接在js页面,调用C#方法

function SetLoginAccount(data) {
    cefAsyncJS.setLoginAccount(data);
}

function GetLoginAccount() {
    return cefAsyncJS.getLoginAccount();
}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有