步骤一:在silverlight后台代码类中声明被js调用的方法
首先要在构造函数中添加
HtmlPage.RegisterScriptableObject("HtmlDemo", this);
其次声明被js调用的方法:
[ScriptableMember] public void UpdateText(string htmlSource) { if (htmlSource != null) { this.richTextBox.HTML = htmlSource; } }
在此声明在何时调用js方法(这里试在按钮1点击时):
private void button1_Click(object sender, RoutedEventArgs e) { //int width = 620; //int height = 220; //HtmlElement heScript = HtmlPage.Document.CreateElement("script"); //heScript.SetAttribute("type", "text/javascript"); //string scriptStr = "function ShowModal(uristring)"; //scriptStr += "{"; //scriptStr += "var result = window.ShowModalTest(uristring,"; //scriptStr += "'dialogWidth:" + width.ToString() + "px; dialogHeight:" + height.ToString() + "px; center:yes');}"; //heScript.SetProperty("text", scriptStr); //HtmlPage.Document.Body.AppendChild(heScript); Uri u = new Uri(Application.Current.Host.Source, "../FckeditorDemo.aspx"); ScriptObject showModal = HtmlPage.Window.GetProperty("ShowModalTest") as ScriptObject; showModal.InvokeSelf(u.OriginalString); }
步骤二:在html页面声明js方法
function ShowModalTest(form) { var result = window.showModalDialog(form,"dialogWidth:620px; dialogHeight:320px; center:yes"); var silverlightObject = document.getElementById("mySilverlightControl"); silverlightObject.content.HtmlDemo.UpdateText(result); }