JS和C#通讯,Html和.net (winform)通讯

1、js调用C#方法,js传参给C#html

html里js代码:函数

    commitDataToC("js传数据给C#");
//把数据传给C#
    function commitDataToC(str) {
//        alert(str);
        window.external.SaveProgress(str);
    }

winform里C#代码:spa

//为了使网页可以与winform交互 将com的可访问性设置为真
 [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
 [System.Runtime.InteropServices.ComVisibleAttribute(true)]
 public void SaveProgress(string str)
 {
    MessageBox.Show("html在调用C#中的方法。SaveProgressstr=" + str);
 }

 

2、C#调用js方法,C#传参给jscode

winform里C#代码:orm

//为了使网页可以与winform交互 将com的可访问性设置为真
 [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
 [System.Runtime.InteropServices.ComVisibleAttribute(true)]
 if (Wbr.Document != null) Wbr.Document.InvokeScript("WfToHtml", objArray);

html里js代码:htm

  //C#传数据给js
    function WfToHtml() {
        alert("wf调用html里面的js函数");
    }

 

注意:js里不能添加console代码,否则在winform里C#会接收不到js传过来的参数,js调用C#代码也会失效。blog

参考:ip

http://www.cnblogs.com/zeroLove/p/3912460.htmlget

相关文章
相关标签/搜索