首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > CAD教程 >

html里怎样用JavaScript调用silverlight里的方法?该怎么处理

2012-02-20 
html里怎样用JavaScript调用silverlight里的方法?如题,求详细代码?急![解决办法]HTML code!DOCTYPE html

html里怎样用JavaScript调用silverlight里的方法?
如题,求详细代码?急!

[解决办法]

HTML code
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<!-- saved from url=(0014)about:internet -->
<head>
  <title>Silverlight Project Test Page </title>

  <style type="text/css">
  html, body {
  }
  body {
  padding: 0;
  margin: 0;
  }
  #silverlightControlHost { 
  }
      .highlightedParagraph
    {
    color: White;
    background-color: Lime;     
    }
  </style>
   
 
  <script type="text/javascript">
    function onSilverlightError(sender, args) {
      if (args.errorType == "InitializeError")  {
        var errorDiv = document.getElementById("errorLocation");
        if (errorDiv != null)
          errorDiv.innerHTML = args.errorType + "- " + args.errorMessage;
      }
    }
  </script>
 
  <script type="text/javascript">
  /////调用Silverlight中函数
  function changeText()
  {
    var control = document.getElementById("silverlightControl");
    control.content.Page.ChangeText("This TextBlock has been updated through JavaScript.");
  }
  /////调用Silverlight中函数
  function getRandom1To6()
  {
    var control = document.getElementById("silverlightControl");
    var random = control.content.services.createObject("RandomNumbers");
    alert("Your number is: " + random.GetRandomNumberInRange(1, 6));
  }
  </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="silverlightControlHost">
<object data="data:application/x-silverlight," type="application/x-silverlight-2" width="400" height="300" id="silverlightControl">
<param name="source" value="ScriptableSilverlight.xap"/>
<param name="onerror" value="onSilverlightError" />
<param name="background" value="white" />

<a href="http://go.microsoft.com/fwlink/?LinkID=108182" 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>
  /////调用Silverlight中函数
  <p onclick="changeText()">Click here to change the Silverlight TextBlock. </p> 
  <p onclick="getRandom1To6()">Click here to get a random number from 1 to 6. </p> 


</body>
</html>



C# code
using System;using System.Collections.Generic;using System.Linq;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using System.Windows.Browser;namespace ScriptableSilverlight{    [ScriptableType()] /////////////要加这个    public partial class ScriptableSilverlight : UserControl    {        public ScriptableSilverlight()        {            InitializeComponent();            HtmlPage.RegisterScriptableObject("Page", this);            HtmlPage.RegisterCreateableType("RandomNumbers", typeof(RandomNumbers));        }        [ScriptableMember()]/////////////要加这个        public void ChangeText(string newText)        {            lbl.Text = newText;        }    }    [ScriptableType()]/////////////要加这个    public class RandomNumbers    {        private Random random = new Random();        [ScriptableMember()]/////////////要加这个        public int GetRandomNumberInRange(int from, int to)        {            return random.Next(from, to+1);        }    }} 

热点排行