Page.xaml代码
<Canvas x:Name="parentCanvas"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="Page_Loaded"
x:Class="JSInteration.Page;assembly=ClientBin/JSInteration.dll"
Width="640"
Height="480"
Background="White"
>
<TextBlock x:Name="txt" Text ="Click Me" Canvas.Left="100" Canvas.Top="100" MouseLeftButtonDown="OnClick"></TextBlock>
</Canvas>
Page.xaml.cs代码

using System.Windows.Browser;///*,需要添加

namespace JSInteration
{
[Scriptable]///必须添加此标签,才能在script中调用该类
public partial class Page : Canvas
{
[Scriptable]///必须添加此标签,才能在script中添加对该事件的处理函数
public event EventHandler TxtClicked;

public void Page_Loaded(object o, EventArgs e)
{
// Required to initialize variables
InitializeComponent();
///将此对象注册到script环境,其中TxtClickApp为注册名,很关键,依赖此名称进行定位
WebApplication.Current.RegisterScriptableObject("TxtClickApp", this);

}
void OnClick(object sender, MouseEventArgs e)
{
TextBlock tB = (TextBlock)this.FindName("txt");
tB.FontSize += 1;

if (TxtClicked != null)
{
TxtClicked(this, e);
}
}
[Scriptable]///必须添加此标签,才能在script中调用该方法
public string GetFontSize()
{
TextBlock tB = (TextBlock)this.FindName("txt");
return Convert.ToInt16(tB.FontSize).ToString();
}
}
}
TestPage.html.js下面的注册JS代码
function createSilverlight()
{
Silverlight.createObjectEx({
source: "Page.xaml",
parentElement: document.getElementById("SilverlightControlHost"),
id: "SilverlightControl",
properties: {
width: "100%",
height: "100%",
version: "1.1",
enableHtmlAccess: "true"
},
events: {
onLoad: OnLoaded
}
});
// Give the keyboard focus to the Silverlight control by default
document.body.onload = function() {
var silverlightControl = document.getElementById('SilverlightControl');
if (silverlightControl)
silverlightControl.focus();
}
}
///XAML加载事件处理函数
function OnLoaded(sender, args)
{
///使用Javascript注册托管代码中的事件处理函数,处理函数JTxtClicked为Javascript中的函数
sender.Content.TxtClickApp.TxtClicked = JTxtClicked;
}

function JTxtClicked()
{
///调用托管代码中函数获取字体大小
var silverlightControl = document.getElementById('SilverlightControl');
alert("Current Font Size:" + silverlightControl.Content.TxtClickApp.GetFontSize());
}
<Canvas x:Name="parentCanvas"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="Page_Loaded"
x:Class="JSInteration.Page;assembly=ClientBin/JSInteration.dll"
Width="640"
Height="480"
Background="White"
>
<TextBlock x:Name="txt" Text ="Click Me" Canvas.Left="100" Canvas.Top="100" MouseLeftButtonDown="OnClick"></TextBlock>
</Canvas>Page.xaml.cs代码

using System.Windows.Browser;///*,需要添加
namespace JSInteration
{
[Scriptable]///必须添加此标签,才能在script中调用该类
public partial class Page : Canvas
{
[Scriptable]///必须添加此标签,才能在script中添加对该事件的处理函数
public event EventHandler TxtClicked;
public void Page_Loaded(object o, EventArgs e)
{
// Required to initialize variables
InitializeComponent();
///将此对象注册到script环境,其中TxtClickApp为注册名,很关键,依赖此名称进行定位
WebApplication.Current.RegisterScriptableObject("TxtClickApp", this);
}
void OnClick(object sender, MouseEventArgs e)
{
TextBlock tB = (TextBlock)this.FindName("txt");
tB.FontSize += 1;
if (TxtClicked != null)
{
TxtClicked(this, e);
}
}
[Scriptable]///必须添加此标签,才能在script中调用该方法
public string GetFontSize()
{
TextBlock tB = (TextBlock)this.FindName("txt");
return Convert.ToInt16(tB.FontSize).ToString();
}
}
}TestPage.html.js下面的注册JS代码
function createSilverlight()
{
Silverlight.createObjectEx({
source: "Page.xaml",
parentElement: document.getElementById("SilverlightControlHost"),
id: "SilverlightControl",
properties: {
width: "100%",
height: "100%",
version: "1.1",
enableHtmlAccess: "true"
},
events: {
onLoad: OnLoaded
}
});
// Give the keyboard focus to the Silverlight control by default
document.body.onload = function() {
var silverlightControl = document.getElementById('SilverlightControl');
if (silverlightControl)
silverlightControl.focus();
}
}
///XAML加载事件处理函数
function OnLoaded(sender, args)
{
///使用Javascript注册托管代码中的事件处理函数,处理函数JTxtClicked为Javascript中的函数
sender.Content.TxtClickApp.TxtClicked = JTxtClicked;
}
function JTxtClicked()
{
///调用托管代码中函数获取字体大小
var silverlightControl = document.getElementById('SilverlightControl');
alert("Current Font Size:" + silverlightControl.Content.TxtClickApp.GetFontSize());
}


[Scriptable]
浙公网安备 33010602011771号