The zen of Silverlight

I just finished teaching Pluralsight's 3-day course on ASP.NET Ajax, and one of the things we now include in the course is coverage of Silverlight. In my opinion, this is one of the most exciting pieces of technology to come along for Web developers in years. More than ASP.NET 2.0, more even than the many slick features of ASP.NET Ajax…

If you haven't seen it demoed or tried it out yet, take a minute and just walk through the following steps:

  1. Download and install Silverlight and the Feb. 2007 CTP SDK (http://www.microsoft.com/silverlight/asp/downloads.aspx).
  2. Create a new Web site in Visual Studio 2005.
  3. Copy the aghost.js file from C:\Program Files\Microsoft SDKs\WPFE\quickstart\samples to your new Web site directory (note there is also a VS template you can install to automate some of these steps in WPFE\Tools).
       aghost = ag + host = 'silver' + host, get it? (thanks to Chris Bowen for illuminating me :)
  4. In your Default.aspx file's element add a reference to the aghost.js file:
    <script type="text/javascript" src="aghost.js"></script>
    
  5. In your <form> element, add the following <div> and script
    <div id="slControl1Host">
    </div>
           
    <script type="text/javascript">
      new agHost("slControl1Host", // hostElementID
                 "slControl1",   // ID of the ActiveX control 
                 "1000",         // Width
                 "1000",         // Height
                 "White",        // Background color
                 null,           // SourceElement (name 
                                 // of script tag containing xaml)
                 "spheres.xaml", // Source file
                       "true",   // IsWindowless
                       "30",     // MaxFrameRate
                       null      // OnError handler    
                );
    </script>
  6. Now, add a new text file to your Web site named spheres.xaml, and populate it with the following XAML:
    <Canvas 
    	xmlns="http://schemas.microsoft.com/client/2007"
    	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Name="mainCanvas">
    	<Ellipse Canvas.Left="100" Canvas.Top="100" Width="200" Height="200"
    		MouseLeftButtonDown="javascript:onButtonDown">
    		<Ellipse.Fill>
    			<RadialGradientBrush GradientOrigin="0.75,0.25" Center="0.5,0.5"
    			  RadiusX="0.5" RadiusY="0.5">
    				<GradientStop Color="Yellow" Offset="0" />
    				<GradientStop Color="Green" Offset="1" />
    			</RadialGradientBrush>
    		</Ellipse.Fill>
    	</Ellipse>
    	
    </Canvas>
    

  7. Finally, go back to your Default.aspx file, and add an inline script block in your element defining the onButtonDown handler - be prepared, this is the zen part!
     <script type="text/javascript">
        function onButtonDown(sender, args)
        {
            sender.Width += 10;
            sender.Height += 10;
        }
    </script>
    

  8. Now run your page and click on the green sphere repeatedly...

Suddenly a whole new world is available, all of it accessible through standard languages we've been using for years, and all of it easy for existing clients to consume. I haven't had this much fun programming since I first tried graphics programming with off-screen bitmaps on a black and white Macintosh :)



Posted Apr 30 2007, 11:20 AM by fritz-onion
Filed under:

Comments

Mark Finkle wrote re: The zen of Silverlight
on 04-30-2007 10:10 AM
"this is one of the most exciting pieces of technology to come along for Web developers in years"

Really? Don't get me wrong, I think the video aspects will be a great boost for the web. However, VML, SVG, canvas+JS have the ability to this too.
Fritz Onion wrote re: The zen of Silverlight
on 04-30-2007 11:13 AM
That's a good point, Mark - I guess it's because of the contrast with Flash where you are so isolated from the DOM that it feels new and fresh. Combine that with a unified graphics model that integrates video, and the fact that it uses the same markup format as desktop apps, and it feels like an easier technology to adapt and justify to decision-makers.

Craig wrote re: The zen of Silverlight
on 04-30-2007 11:42 AM
I find the Silverlight subset of XAML to be disappointingly restrictive. Where, for example, is FlowDocument? I just can't think of anything I'd want to do that only involves gradient-filled elipses (hyperbole, yes, but my point remains).

Also, re the comment, "standard languages we've been using for years". You've been using XAML for years? :)
Fritz Onion wrote re: The zen of Silverlight
on 04-30-2007 4:35 PM
FlowDocument is sorely missing, I agree. I'm thinking Flash-like functionality myself - interactive animations will be the most immediately applicable.

As for standard languages, I was refering to JavaScript and C#, not XAML.
Darren Kopp wrote re: The zen of Silverlight
on 05-01-2007 8:16 AM
@ mark finkle: go check out silverlight 1.1. silverlight is more than just video.
Joshua Flanagan wrote re: The zen of Silverlight
on 05-01-2007 1:05 PM
To be clear - this demo doesn't have anything to do with ASP.NET, does it?
Could I eliminate step #2 and change Default.aspx to Default.htm to get the same result?
I only ask, because my beta/CTP/junk machine doesn't currently have VS2005 installed.
Fritz Onion wrote re: The zen of Silverlight
on 05-01-2007 1:09 PM
That's correct, Joshua - you can use straight .htm and javascript no problem.

Joshua Flanagan wrote re: The zen of Silverlight
on 05-01-2007 2:39 PM
Hrm... I'm getting javascript errors. (Running on Vista - does that matter?)
Can you post the entire HTML file that I should end up with?
jbinaz wrote re: The zen of Silverlight
on 05-02-2007 7:54 AM
@Joshua: I'm running on Vista and getting JS errors as well. "Undefined is null or not an object", to be specific. It has something to do with the versionStr variable. I haven't had time to dig into it yet, but I'm wondering if it also has something to do with IE. I may see about starting it from VS 2005 with Firefox to see if I run into the same issue.

jbinaz
jbinaz wrote re: The zen of Silverlight
on 05-02-2007 8:00 AM
@Joshua again: it works in Firefox. I didn't even change my browser in VS 2005, I simply browsed to the URL ("http://localhost:49616/", your port may differ) that VS 2005 was using to run my project and it ran just fine. Probably a security setting on IE.
Onion Blog wrote The zen of Silverlight (Beta 1.0 update)
on 05-02-2007 8:30 AM

Add a Comment

(required)  
(optional)
(required)  
Remember Me?