Viewer

 

用JS调用Silverlight 3.0的托管代码


1. 在用户控件中定义一个ScriptableMember方法:

1         [ScriptableMember()]
2         public void InitHotel(int hotelID)
3         {
4             InitHotelLanguages(hotelID);
5             InitMapPositions(hotelID);
6         }
 

2. 在此控件的构造函数中将其注册为Scriptable对象:

1     System.Windows.Browser.HtmlPage.RegisterScriptableObject("SilverlightMainPage", this);

 

3.  用JavaScript通过SilverlightHost调用在托管代码中定义的ScriptableMember方法

 

1         $("#btnChangeHotel").click(function() {
2             var hotelID = parseInt($("#txtHotelID").val());
3             if (!isNaN(hotelID)) {
4                 $("#mapTool")[0].Content.SilverlightMainPage.InitHotel(hotelID);
5                 $("#txtHotelID").val(hotelID);
6             }
7         });


 

3. 在页面的Silverlight object对象的参数中加入<param name="onLoad" value="onLoad" />


4. 定义onLoad方法并保存Silverlight对象SilverlightHost

1         var SilverlightHost = null;
2         function onLoad(sender) {
3             SilverlightHost = sender.getHost();
4         }


5.  用JavaScript通过SilverlightHost调用在托管代码中定义的ScriptableMember方法

1         $("#btnChangeHotel").click(function() {
2             var hotelID = parseInt($("#txtHotelID").val());
3             if (!isNaN(hotelID)) {
4                 SilverlightHost.Content.SilverlightMainPage.InitHotel(hotelID);
5                 $("#txtHotelID").val(hotelID);
6             }
7         });

 

参考:

http://www.silverlight.net/learn/quickstarts/htmlbridge/ 

http://blog.csdn.net/net_lover/archive/2008/04/10/2272115.aspx

http://forums.silverlight.net/forums/p/158766/376386.aspx 

 

 

posted on 2010-03-30 14:08  Viewer  阅读(287)  评论(0编辑  收藏  举报

导航