In an earlier post I described how you can call jQuery from within a Silverlight application. The process is rather simple as was the example. Please refer to the original post for further details.
Afterwards I began to think about calling more complex jQuery constructs from within Silverlight. What about registering events and using anonymous functions? At first glance it didn’t seem it was going to work at least not with the technique I described in my earlier post. Then I discovered the dark side of Javascript the Eval function. The Silverlight HtmlPage.Window object exposes the Eval function that allows you to evaluate any string.
I added the following line of code to my the Page constructor for the example from my previous post.
21 HtmlPage.Window.Eval("$('#hello').click(function() { alert('Eureka'); });");
This code basically registers a click event with the DOM element with id of #hello. When the DOM element is clicked an alert is displayed and it works perfectly well.
As with raw Javascript all the pros and cons with the use of Eval exist when used from within Silverlight. But it’s good to know that if required complex Javascript functionality can be implemented from within Silverlight.