In this post I teach you how you can integrate JavasScript in your Silverlight apps. Sample source code available for download.
Without the slightest of doubts it has been concluded in the .NET developers community that, Silverlight is great platform to build LOB applications. It has taken all the developers by storm since its inception just three years ago. Web, Desktop and now mobile a Silverlight app thrives in every space. In this post I will talk about one of my favourite feature in this great technology. It is about the browser integration and how can you play with DOM.
I also showcase a very cool example to demonstrate this feature. The complete solution is available for download at the end of this post. So let me first talk about the sample app that we are going to build. We shall be using Bing Web Api. I won’t getting into much details of this api but will redirect you to the right place if you want to learn more about this. So we will develop a Silverlight app that will allow the user to enter their search query in the TextBox and clicking on the Search button , the Bing Api using JSON protocol will fetch us the results.
These results will be then displayed in the ListBox of the application. Refer to the screen shot of the application below
So let us get started with it.
SILVERLIGHT AND BROWSER INTEGRATION
First thing first, to enable this integration you must ensure that ‘enableHtmlAccess’ attribute is set to true in your Silverlight plugin code.
I have the Search function which has one parameter , searchText. This function will be called from the Silverlight code. This function triggers a callback function called SearchCompleted, let us have look into this function now.
Notice this class is decorated with ScriptableType attribute which makes all this possible. If you miss these attributes you would get error. Let us now jump to now our other class which binds the ListBox in Silverlight app.
for binding the ListBox.
Please note in order to run the application you must first insert your App Id from Bing. Its a two minute process and the registration is free. Create your Bing Developer App id from here
. After you have got your id then search for the line ‘Insert your App id over here before you run’ in the SilverlightBrowserIntegrationTestPage.aspx page and replace this text with your App id. Have a great day. Cheers !