8,735
社区成员
发帖
与我相关
我的任务
分享
<TextBlock x:Name="txbTest" Height="20"/>
namespace SilverlightLoad_Javascrtip
{
[ScriptableType] //增加ScriptableType,表示此类可以让javascript调用
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
HtmlPage.RegisterScriptableObject("page", this);//注册对象
}
[ScriptableMember]
public void ChangeText(string newText) //增加ScriptableMember,表示此方法可以让javascript调用
{
txbTest.Text += newText;
}
}
}
<script type="text/javascript">
function updateSilverlightText() {
var ele = document.getElementById("silvercontrol1");
alert("call ailverlight");
ele.content.page.ChangeText("This text has been changed");
}
</script>
<form id="form1" runat="server" style="height:100%">
<div id="silverlightControlHost">
<object id="silvercontrol1" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
<param name="source" value="ClientBin/SilverlightLoad Javascrtip.xap"/>
<param name="onError" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="4.0.50826.0" />
<param name="autoUpgrade" value="true" />
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50826.0" style="text-decoration:none">
<img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="获取 Microsoft Silverlight" style="border-style:none"/>
</a>
</object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>
<div id="myEle">
<p onclick="updateSilverlightText()">
Click here to change the text in silverlight.
</p>
</div>
</form>