`
jiasongmao
  • 浏览: 650937 次
  • 性别: Icon_minigender_1
  • 来自: 石家庄
社区版块
存档分类
最新评论

silverlight和js相互调用

阅读更多

步骤一:在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);
          }

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics