Communicating between Javascript and Silverlight is, fortunately, relatively straight forward. The following sample demonstrates how to make the call both ways.
Calling Silverlight From Java script:
Calling Javascript From Silverlight:
Example for both:
Page.xaml:
namespace SilverlightApplication
{
public partial class Page : UserControl
public Page()
InitializeComponent();
HtmlPage.RegisterScriptableObject("Page", this);
HtmlPage.Window.Invoke("TalkToJavaScript", "Hello from Silverlight");
}
[ScriptableMember]
public void UpdateText(string result)
myTextbox.Text = result;
Default.aspx:
<script type="text/javascript">
function TalkToJavaScript( data)
alert("Message received from Silverlight: "+data);
var control = document.getElementById("silverlightControl");
control.Content.Page.UpdateText("Hello from Javascript!");
</script>
<UserControl x:Class="SilverlightApplication7.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock x:Name="myTextbox">Waiting for call...</TextBlock>
</Grid>
</UserControl>
Thank you, --Mike Snow Subscribe in a reader
PingBack from http://www.tmao.info/silverlight-tip-of-the-day-15-%e2%80%93-communicating-between-javascript-silverlight/