.NET Blog

Tony Cavaliere

 
My Favourite Albums
  And the Grappa wins.
E-mail me Send mail

Disclaimer

Hey unlike other bloggers I stand by what I say but just in case. The opinions expressed herein are my own except on Tuesday when the second card is not turned up otherwise it ain't worth squat.

© Copyright 2012

Calling jQuery directly from Silverlight

Last night I gave a jQuery presentation at TVBUG and I was asked whether you could call the jQuery API directly from within Silverlight. My initial response was yes, but I had promise the person that I would create a demo and blog about it.

Calling Javascript from within Silverlight control involves the following steps;

  1. Add a using System.Windows.Browser namespace to the code behind of the Silverlight control.
  2. Creating a ScriptObject by calling HtmlPage.Window.CreateInstance.
  3. Calling the Invoke method off the ScriptObject from step 2.

Let’s look at some code.

The listing below contains a code snippet that is run when a Silverlight button is clicked.

   23  private void btn_Click(object sender, RoutedEventArgs e)

   24  {

   25    ScriptObject js = HtmlPage.Window.CreateInstance("$", new string[] { "#hello" });

   26    js.Invoke("css", "background-color", "#00FF00");

   27    js.Invoke("css","border","5px solid #FF0000");

   28  }

Notice the parameters to the HtmlPage.Window.CreateInstance static member shown on line 25 contains the call to jQuery function $() and the selector #hello. This call returns a ScriptObject and can subsequently be used to invoke functions off the jQuery object. In this example, I am first setting the background of the DOM element with id of hello to red and then setting it’s border.

I have a working demo of this just navigate to this site.  

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: jQuery | Silverlight
Posted by CynotWhyNot on Wednesday, March 18, 2009 4:36 PM
Permalink | Comments (5) | Post RSSRSS comment feed

Related posts

Comments

blog.jquery.com

Saturday, March 28, 2009 9:22 AM

pingback

Pingback from blog.jquery.com

jQuery: » This Week in jQuery, vol. 3

iliadobrev.com

Saturday, March 28, 2009 3:53 PM

pingback

Pingback from iliadobrev.com

My Space In The Net » Blog Archive » JQuery Resources

answerspluto.com

Monday, July 13, 2009 10:05 PM

pingback

Pingback from answerspluto.com

list of urls - 5 « Answers Pluto

discount bags US

Thursday, May 27, 2010 2:34 PM

discount bags

Funny, I actually had this on my mind a few days ago and now I come across your blog.

fendi US

Monday, May 31, 2010 6:57 PM

fendi

Easily, the post is really the greatest on this laudable topic. I concur with your conclusions and will thirstily look forward to your future updates. Saying thanks will not just be sufficient, for the fantasti c lucidity in your writing. I will instantly grab your rss feed to stay privy of any updates. Solid work and much success in your business enterprize!