A big part of the excitement around Silverlight 1.1 is the client-side .NET runtime, which lets you write client side code in C#. Silverlight 1.0’s programmability story is Javascript only. However, there’s a way to write your Silverlight 1.0 code in C#, using the Script# Compiler.
Nikhil Kothari (the author of Script#) has a great post on writing a Photo Carousel in Silverlight 1.0 with Script#. I’ll run over the background and usage of Script#, but my main goal is to convince you to go read more on Nikhil’s blog and the Script# project page. Nikhil is an architect on the Web Platform and Tools team at Microsoft, responsible for the Webforms features area. He’s been with ASP.NET since it was called XSP, so I think we can trust him. Oh, hey, my reader boredom sensors just went off! Time for a screenshot of the carousel control Nikhil created with Script# and Silverlight:
The magic of Script#
Script# compiles C# code to Javascript, so you can write your entire application in C#. Javascript is a great language (better than C# in some respects), but by writing in C# you can take advantage of some things that just aren’t available in Javascript, such as:
- Visual Studio’s support for C# refactoring
- Contextual output from a common code base – for instance, a debug build includes method names for what would otherwise be anonymous methods to aid in debugging, while a release build can output minimized code which removes whitespace and uses short identifier names.
- Smooth integration with ASP.NET AJAX
- IntelliSense and compile time checking
- Library support for the kinds of things you’ll want to be doing in ASP.NET Javascript like Cross Domain AJAX with JSONP, Silverlight, UI Controls and Behaviors, RSS, and managed HTTP Request / Response handling
- Did I mention you can write an entire Silverlight 1.0 application in C#?
Installing Script# gives you a Script# project type, which includes both the binaries and the compilation magic:
If you’re writing Silverlight Javascript code, you’ll want to set a reference to ssagctrl.dll (as in Script Sharp Ag Control – Ag being the chemical symbol for Silver), which includes the metadata for the Silverlight Javascript interfaces. That will give you access to classes like System.Silverlight.Presentation, complete with IntelliSense for all supported objects.
The simplest way to use Script# is by adding a Scriptlet control to your page:
Clicking the “Edit C# Code…” Scriptlet Task brings up an editor window:
When your project is compiled, the Scriptlet code is automatically compiled to Javascript and included in your page.
You can also create a Script# class library by adding a Script# class file to your project (which includes the correct build settings), the write standard C# class code:
public sealed class PhotoControl : IDisposable {
private Canvas _containerCanvas;
private Canvas _frameCanvas;
private Image _image;
public PhotoControl(Canvas rootCanvas) {
// Find the interesting XAML elements we want to program against
_containerCanvas = (Canvas)rootCanvas.FindName("photo");
_frameCanvas = (Canvas)_containerCanvas.FindName("photoFrame");
_image = (Image)_containerCanvas.FindName("photoImage");
// Center the photo within the silverlight control
SilverlightControl control = rootCanvas.GetHost();
Canvas.SetLeft(_containerCanvas, (control.Content.ActualWidth - _containerCanvas.Width) / 2);
Canvas.SetTop(_containerCanvas, (control.Content.ActualHeight - _containerCanvas.Height) / 2);
}
public string PhotoUri {
get { return _image.Source; }
set { _image.Source = value; }
}
public void Dispose() {
if (_containerCanvas != null) {
_image = null;
_frameCanvas = null;
_containerCanvas = null;
}
}
}
When you build, your code is automatically converted to Javascript and placed in your App_Scripts folder.
How does it all work?
Script# includes its own compiler, ssc.exe. You can call it directly, but the preferred approach is to use the Script# MSBuild .targets file (which is included automatically when you use the Script# project templates). Since your code is C#, it’s compiled in both the C# compiler (csc.exe), mostly to check for compilation errors, but also to generate a DLL which can be used in debugging, via reflection, etc. It’s also compiled by the Script# compiler (ssc.exe), which outputs Javascript in the appropriate format.
