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.
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.
In the code-behind I subscript to the WebBrowser’s ScriptNotify event and implement an EventHandler.
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.
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.
You can download the demo project here.
by xamlgeek
One Response to “ScriptNotify in Silverlight 4”