深圳政协委员钟帆飞诈骗30亿

主持正义

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

一、 在Silverlight注册一个Javascript脚本对象,并将C#方法暴露出来,就可以了。

注册脚本对象:

HtmlPage.RegisterScriptableObject("Builder",this);

定义脚本成员:

[ScriptableMember]
public void CreateRect(int width,int height)
{
   Rectangle rect = new Rectangle();
   rect.width = width;
   rect.height = height; 
   rect.Fill = new SolidColorBrush(Colors.Blue);
   LayoutRoot.Children.Claer();
   LayoutRoot.Children.Add(rect);
}

二、 在Html中定义一个Silverlight对象,通过javascript获取此对象,用此对象来调用CreateRect方法创建矩形。

创建Silverlight对象:

<Object id="XamlObject" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="400" height="300">
<param name="source" value="ClientBin/Sample.xap"/>
<param name="onerror" value="onSilverlightError"/>
<param name="background" value="white"/>
<param name="minRuntimeVersion" value="2.0.31005.0"/>
<param name="autoUpgrade" value="true"/>
</Object>

通过点击按钮,调用该对象的方法:

<!--html-->
<input id="button1" type="button" value="生成矩形" onclick="createRectangle();">
<!--javascript-->
function createRectange(){
   var xamlObject = document.all('xamlObject');
   xamlObject.content.Builder.CreateRect(100,200);
}

 

posted on 2012-06-17 14:18  jayce80  阅读(492)  评论(0编辑  收藏  举报