silverlight与javascript交互操作

C#端代码:

String text = "lenny";
string text2 = "dou";
HtmlPage.Window.Invoke("calledBySL2", new object[] { text, text2 });

 

Html端代码:

function calledBySL2(obj, obj2) {
       alert("Hello: " + obj + ", " + obj2);

}

 

-----------------------------------------------------------------------------------------

Javascript调用Silverlight代码

 

C#代码:

首先需要注册一个脚本对象

HtmlPage.RegisterScriptableObject("JavaMachine", this);

 

然后写函数

[ScriptableMember]
public string AddNum(string str)
{
            showTB.Text = str;
            return "你懂的";
 }

 

Javascript代码:

function callSL() {
      var sl = document.getElementById("sl_obj");
      var msg = sl.Content.JavaMachine.AddNum("abcdef");
      alert("msg: " + msg);
}

 

然后在HTML代码嵌入的<object id="sl_obj">中加入id的名字 sl_obj

posted @ 2011-09-26 16:29  Areas  阅读(262)  评论(0编辑  收藏  举报