學Silverlight 2系列(21):如何在Silverlight中調用JavaScript

來源:互聯網
上載者:User

Silverlight中內建了對於HTML、用戶端指令碼等的支援。很多情況下,我們編 寫的Web應用程式中用了一些JavaScript或者AJAX架構,我們希望能夠在 Silverlight調用某些指令碼方法,或者說在Silverlight中觸發某個指令碼的執行, 這時就需要用到在Silverlight中調用JavaScript,本文將簡單介紹這一內容。

使用GetProperty擷取指令碼對象

先來看一個簡單的例子,在Silverlight測試頁面中放入一個div用作顯示信 息:

<div id="result"></div>

編寫一段簡單的JavaScript代碼:

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

再編寫一個簡單的輸入資訊介面:

<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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.