The following example shows the code for the test page (HBTestPage.aspx), the MyScriptableManagedType class, and the App.xaml code-behind.
Code
<!-- HBTestPage.aspx -->
<%@ Register Assembly="System.Web.Silverlight" Namespace="System.Web.UI.SilverlightControls"
TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html style="height: 100%;">
<head runat="server">
<title>Test Page For HB</title>
<script type="text/javascript">
var slCtl = null;
function pluginLoaded(sender){ // ASPX version
slCtl = sender.get_element();
alert(slCtl.Content.mySLapp.MyToUpper("Test String"));
}
function Button1_onclick() {
slCtl.Content.mySLapp.Name = navigator.appName;
alert(slCtl.Content.mySLapp.Name);
}
</script>
</head>
<body style="height: 10%; margin: 0;">
<form id="form1" runat="server" style="height: 100%;">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div style="height:10%;">
<asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/HB.xap" MinimumVersion="2.0"
Width="100%" Height="100%" OnPluginLoaded="pluginLoaded" />
</div>
<input id="Text1" type="text" disabled="disabled"
value="My Initial Value" /><br />
<input id="Button1" type="button" value="Test set/get"
onclick="return Button1_onclick()" />
</form>
</body>
</html>
' MyScriptableManagedType.vb
Imports System.Windows.Browser
'<ScriptableType()> _
Public Class MyScriptableManagedType
Dim _name As String
<ScriptableMember()> Public Function MyToUpper(ByVal str As String) As String
Return str.ToUpper
End Function
<ScriptableMember()> Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
End Class
// MyScriptableManagedType.cs
using System.Windows.Browser;
namespace HB {
public class MyScriptableManagedType {
[ScriptableMember()]
public string MyToUpper(string str) {
return str.ToUpper();
}
[ScriptableMember()]
public string Name { get; set; }
}
}
Imports System.Windows.Browser
Partial Public Class App
Inherits Application
Public Sub New()
InitializeComponent()
End Sub
Private Sub Application_Startup(ByVal o As Object, ByVal e As StartupEventArgs) Handles Me.Startup
Me.RootVisual = New Page()
'HtmlDocument requires Imports System.Windows.Browser
Dim doc As HtmlDocument = HtmlPage.Document
doc.GetElementById("Text1").SetProperty("disabled", False)
doc.GetElementById("Text1").SetAttribute("value", _
"This text set from managed code.")
'Set up some scriptable managed types for access from Javascript.
Dim smt As MyScriptableManagedType = New MyScriptableManagedType()
HtmlPage.RegisterScriptableObject("mySLapp", smt)
End Sub
End Class
// App.xaml.cs
using System.Windows;
using System;
using System.Windows.Browser;
namespace HB {
public partial class App : Application {
public App() {
this.Startup += this.Application_Startup;
InitializeComponent();
}
private void Application_Startup(object sender, StartupEventArgs e) {
// Load the main control
this.RootVisual = new Page();
// HtmlDocument requires using System.Windows.Browser;
HtmlDocument doc = HtmlPage.Document;
//Enable the HTML textbox on the page and say hello
doc.GetElementById("Text1").SetProperty("disabled", false);
doc.GetElementById("Text1").SetAttribute("value",
"This text set from managed code.");
//Set up some scriptable managed types for access from Javascript.
MyScriptableManagedType smt = new MyScriptableManagedType();
HtmlPage.RegisterScriptableObject("mySLapp", smt);
}
}
}