ScriptNotify in Silverlight 4

29 Mar

Silverlight 4 introduces a WebBrowser control which I have written a short post about this control here.

In some scenarios it is necessary to get a response from the WebBrowser control – to do that you can use the ScriptNotify event on the WebBrowser control. The ScriptNotify occurs when the content contained in the WebBrowser control passes a string to the Silverlight plug-in by using JavaScript.

To demonstrate the functionality of ScriptNotify event, I have created a Silverlight 4 project and an ASP.NET webproject that hosts the Silverlight project. I have made the Silverlight project an out-of-browser application – this is necessary to display HTML in the WebBrowser.

On the server-side I have added an HTML-file called HTMLPage1.htm that will be the HTML-file that I displays in the WebBrowser. I have added a button to the page and added a JavaScript function called notify that is invoked when I click the button.

image

In the Silverlight project I have added a WebBrowser control on the MainPage.xaml and I have set the source to our HTML-page on the server.

image

In the code-behind I subscript to the WebBrowser’s ScriptNotify event and implement an EventHandler.

image

To invoke the ScriptNotify event from the HTML-file I need to pass a string to the Silverlight plug-in. The Silverlight hosting environment allows you to call Notify on the Window.external object and pass a string to the plug-in. When you do this, the ScriptNotify event occurs.

image

My ScriptNotify event catches the call from the JavaScript function and from the NotifyEventArgs in the EventHandler I get the string value sent from the JavaScript function.

image

You can download the demo project here.

by xamlgeek

One Response to “ScriptNotify in Silverlight 4”

Trackbacks/Pingbacks

  1. 2010 in review « xamlgeek - January 3, 2011

    […] ScriptNotify in Silverlight 4 March 2010 […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: