Tuesday, April 8, 2008

Javascripting Silverlight 2.0

As the heading implies ,here I am going to explain about "Using Javascript with Silverlight 2.0".

Silverlight 2.0 has got so many exciting features/controls like Calendar,MultiScaleImage etc along with c# support.But what can a javascript developer do?
He can still code in javascript itself and make use of these new things.

Here goes some guidelines to start javascripting in Silverlight 2.0
  1. Since we don't have a built-in Visual studio template for creating Silverlight2.0 application using Javascript ,we have to use the available Template of either C# or VB.Net
  2. Getting reference to Plugin and other objects
<asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/SilverlightApplication2.xap" Version="2.0" Width="100%" Height="100%" OnPluginLoaded="plloaded" />

plloaded is a javascript function where we are going to code rest.
3 . Subscribing to events

<script language="javascript">
   1: function grd_MouseLeftButtonDown(sender,args){
   2:     alert("Clicked on " + sender.name);
   3: }
   4: function plloaded(sender,th)
   5: {
   6:     var i=sender.get_element();
   7:     var grd=i.Content.findName("LayoutRoot");//LayoutRoot is the Grid Present in Page.Xaml 
   8:     grd.AddEventListener("MouseLeftButtonDown", "grd_MouseLeftButtonDown")
   9: }

Its done this will invoke the grd_MouseLeftButtonDown function on mouse click of Grid present in Xaml.

A sample which implements this method is located here

No comments: