Shine that Silver Light On Me
Getting back to some more Windows/Microsoft features after our brief excursion into Mac territory (no worries, we will get back there too, and soon), we’re shipping full Silverlight support in Chrome ‘Joyride’ 2.0.3 just now. Full support means we’re providing everything you need to build Silverlight 1.1 Alpha apps in Chrome and Visual Studio 2008 – project templates; XAML editor with IntelliSense support; MSBuild support – the full shebang.
Let’s run thru creating a small Silverlight app to see what’s involved!
First, to start off, you will need three prerequisites installed to follow along: Visual Studio 2008 Beta 2, Silverlight 1.1 Alpha Refresh and Silverlight Tools Alpha for Visual Studio 2008 (all available here). Also, of course, you will need at least version 2.0.3 of ‘Joyride’.
Bring up the New Project dialog of Visual Studio and open the Chrome folder to notice the new “Silverlight” folder beneath. We provide two templates for Silverlight – one for a general project, and one for a Class Library that (similar to plain .NET library projects) will allow you to write classes and controls to share between different projects. For now, let’s go with a plain “Silverlight Project”, and call it SilverChrome.
Of course it goes without saying that, as with any Chrome project, you can add additional source files to the project and write any code you please. No more reliance on JavaScript or Flash’s Action Script to implement rich web clients!
Finally, there’s an HTML page in your project that will host your Silverlight page for testing purposes – although for final deployment you’ll most likely embed the Silverlight page into your existing website (much like you might do with a Flash control, now) – as well as two JavaScript helper files that perform the task of actually loading the Silverlight runtime.
Let’s press Ctrl+Shift+B (Build) now and navigate our favorite browser to TestPage.html to see – an empty white page!
That’s right. Our Silverlight page is working fine, but it’s of course not doing anything, because we’re just providing an empty XAML file. Let’s go back now ad add some functionality. First open the Page.xaml file and enter the following tag with the . Try typing instead of copy/pasting it, to see that you get full intellisense for available classes and properties:
Next, open the Page.xaml.pas code-behind file, and add the following method. Note how we can refer to the ClickMe TextBlock in code, even though the only place it is actually defined is the XAML. What’s happening here is that the Silverlight build process generates code from the XAML file during compile, and injects this into the project as a partial class for Page. If you ever want to look a that code (for example to investigate a confusing error message), you can find it in a file named Page.g.pas in the ./obj folder.
method Page.OnClick(sender: Object; e: EventArgs); begin ClickMe.Text := 'Clicked'; end;
Once done, press Ctrl+Shift+B to rebuild, switch back to your browser and refresh the page. You can now see our “Click Me” TextBlock in all it’s glory – and click it to make the magic happen!