I'm having a dandy of a time getting Silverlight and JavaScript to communicate with each other.
In the page.xaml.cs file, I marked the class as ScriptableType and two methods with ScriptableMember. I then declared:
HtmlPage.RegisterScriptableObject("Page", this);
When I attempt to invoke either method, I get obj.Content.Page is undefined:
var obj = document.getElementById('silverlightControl');
alert(obj); // [object HtmlObjectElement]
alert(obj.Content); // Content
alert(obj.Content.Page); // obj.Content.Page is undefined
alert(obj.Content.Page.GetRegion());
On the flip side, inside of a method in page.xaml.cs, I am calling:
HtmlPage.Window.Invoke("mapRegionChanged", GetRegion());
I have a respective method in JavaScript, which never gets called:
function mapRegionChanged(region) {
alert("Region changed: " + region);
}
The Silverlight object is declared as follows:
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2"
width="151px" height="77px" id="silverlightControl">
<param name="onload" value="regionsLoaded" />
<param name="source" value="<%= Url.Content("~/ClientBin/Worldmap.xap") %>" />
<param name="onerror" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="2.0.31005.0" />
<param name="autoUpgrade" value="true" />
<param name="enableHtmlAccess" value="true" />
<a href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;">
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none" />
</a>
</object>
The UserControl is defined in XAML as:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Worldmap.Page"
Width="151" Height="77" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
<Grid x:Name="LayoutRoot" Background="White" Width="151" Height="77" Cursor="Hand" MouseMove="OnMouseMove" MouseLeftButtonUp="OnMouseUp" MouseLeave="OnMouseLeave">