在test。aspx页面中:

eg1。

<head runat="server">
    <title>Test Page For SilverlightApplication6</title>
     <script type="text/javascript">
      function a(message)
    {
    var resultSpan = $get("result"); //由于所用的这个方法必须写在aspx页面中,
    resultSpan.innerText = "Hello "+message;
    }
    </script>

</head>
<body style="height:100%;margin:0;">
    <form id="form1" runat="server" style="height:100%;">
    <div id="result"></div>
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <div  style="height:100%;">
            <asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/SilverlightApplication6.xap" MinimumVersion="2.0.30523" Width="100%" Height="100%" />
        </div>
    </form>
</body>
</html>

但是 javascript同样可以在放在html页面中

  <script type="text/javascript">

    function abc()
    {

       alert("aaa");
    }

</script>
</head>

<body>
    <!-- Runtime errors from Silverlight will be displayed here.
 This will contain debugging information and should be removed or hidden when debugging is completed -->
 <div id='errorLocation' style="font-size: small;color: Gray;"></div>
  <div id="parent">
  </div>
   <div id="result"></div> 同样可以调用
    <div id="silverlightControlHost">    
  <object data="data:application/x-silverlight," type="application/x-silverlight-2-b2" width="100%" Height="200px">
   <param name="source" value="ClientBin/SilverlightApplication6.xap"/>
   <param name="onerror" value="onSilverlightError" />
   <param name="background" value="white" />
   
   <a href="
http://go.microsoft.com/fwlink/?LinkID=115261" style="text-decoration: none;">
        <img src="
http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>
   </a>
  </object>
  <iframe style='visibility:hidden;height:0;width:0;border:0px'></iframe>
    </div>

page页面:

<StackPanel Background="#CDFCAE" Orientation="Vertical">
            <StackPanel Height="40">
                <TextBlock Text="Calling Browser Script from Silverlight"
                   Foreground="Red"></TextBlock>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBox x:Name="input" Width="340" Height="40" Margin="20 0 20 0"></TextBox>
                <Button x:Name="submit" Width="120" Height="40" Background="Red"
            Content="调 用" FontSize="20" Foreground="Red" Click="submit_Click"></Button>
            </StackPanel>
 </StackPanel>

。cs页面
        private void submit_Click(object sender, RoutedEventArgs e)
        {
            ScriptObject hello = HtmlPage.Window.GetProperty("a") as ScriptObject;//使用GetProperty获取脚本对象
           
hello.InvokeSelf(this.input.Text);//调用方法,
        }

 

ScriptObject提供了任何客户端脚本的封装,不仅仅是JavaScript,使用其他的AJAX框架也可以,如jQuery等。然后调用InvokeSelf()方法,传入参数,这里ScriptObject总共提供了两个方法,Invoke和InvokeSelf,如果我们只调用脚本对象的自身,就可以使用InvokeSelf,如果脚本对象中还有其它的函数等,可以使用Invoke传入名称进行调用,两个方法的定义如下:

[SecuritySafeCritical]
public virtual object Invoke(string name, params object[] args);
[SecuritySafeCritical]
public virtual object InvokeSelf(params object[] args);

eg2

js:

     <script type="text/javascript">
    myHello = function(message)
    {
        this.Message = message;
    }
    myHello.prototype.Display = function()
    {
        var resultSpan = $get("result");
        resultSpan.innerText = "Hello " + this.Message;
    }

    </script>

使用HtmlPage.Window属性的CreateInstance方法。还是使用上面的示例,我们在测试页中加入如下一段脚本,使用prototype为myHello添加了显示的功能:cs:
        private void submit_Click(object sender, RoutedEventArgs e)
        {
            ScriptObject script = HtmlPage.Window.CreateInstance("myHello", this.input.Text);

            object result = script.Invoke("Display");

        }

eg1和eg2实现的功能是一样的。

eg3使用HtmlPage.Window.Eval()

page。cs中

private void submit_Click(object sender, RoutedEventArgs e)
{
    HtmlPage.Window.Eval(this.input.Text);
}

 

 

Posted on 2008-09-17 14:14  sunlibo  阅读(200)  评论(0)    收藏  举报