1. Problem
You want to program Silverlight using either the IronRuby, IronPython, or Managed JScript dynamic languages.
2. Solution
Download and install the Dynamic Language Runtime SDK for Silverlight on Windows or on a Mac OS X system.
3. How It Works
The first step is to download the latest Silverlight Dynamic Languages SDK from CodePlex at sdlsdk.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=25120.
This URL takes you to the most recent version available (version 0.5.0
at the time of this writing), so be sure to check the Releases section
to see if a more up-to-date version is available.
Download the agdlr-version# (Everything) package and unzipped the contents to the Code\Ch02_ProgrammingModel\DLR_Download\ folder. Then, copy the contents over to a directory named c:\SagDLR. The package includes the IronRuby and IronPython languages as part of the "everything" download.
NOTE
To program in IronRuby, download Ruby from rubyforge.org/frs/?group_id=167 and install using the OneClick Installer for Windows. It will install Ruby at c:\Ruby on your hard drive.
After installing
the Silverlight Dynamic Languages SDK and Ruby, you can build
applications using the IronRuby, IronPython, or Managed JScript dynamic
languages. At the time of this writing, there are no Visual Studio 2010
templates for dynamic languages. The Silverlight Dynamic Languages SDK
includes a tool named Chiron (Chiron.exe)
that allows you to work with Silverlight and the dynamic languages;
however, there is an additional alternative available for dynamic
language development with Silverlight highlighted at these links:
Creating Interactive Bing Maps with Silverlight and IronRuby
msdn.microsoft.com/en-us/magazine/ee291739.aspx
Back to "Just Text"
ironpython.net/browser/sl-back-to-just-text.pdf
Whatever tool you choose to use to build dynamic Silverlight applications, the code itself remains the same.
4. The Code
The simplest Silverlight
application that uses a dynamic language consists of an HTML or ASPX
file to host the application just as in a compiled Silverlight
application and an app.xaml file that defines the Silverlight UI, much as MainPage.xaml does for a compiled Silverlight application. The codebehind for app.xaml in a dynamic language application can be one of the following, depending on the language:
app.py: The IronPython code-behind file
app.rb: The IronRuby code-behind file
app.jsx: The Managed JScript code-behind file
The ReadMe file that ships
with the Silverlight Dynamic Languages SDK provides some instructions on
how to create a new Silverlight application like the one we describe
here.
To create a new application, open a command prompt, and navigate to the script directory, which in your configuration is c:\SLDLR\script. To create a dynamic language Silverlight application, run the following command, but replace language with ruby, python, or jscript:
sl.bat language <application_name>
Create an application named SilverlightDynamicApp with the IronPython language using this command:
Sl.bat python SilverlightPythonApp
This code creates an application directory in the c:\SLDLR\script\ directory named SilverlightPythonApp. In the SilverlightPythonApp directory, it creates three folders named javascripts, python, and stylesheets, as well as an HTML file named index.html.
The javascripts folder contains an error.js file with the typical onSilverlightError handler in it. The stylesheets directory contains two CSS files named error.css and screen.css: screen.css provides basic styling for the HTML page, and error.css provides highlighting for any errors that occur.
Since you created a Python-based application, the python directory contains app.xaml and app.py. As mentioned earlier, in a dynamic language application, app.xaml contains the UI code and app.py is the codebehind written in IronPython. Listings 1 and 2 show the contents of these files.
Listing 1. Recipe 1's app.xaml File
<UserControl x:Class="System.Windows.Controls.UserControl"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid x:Name="layout_root" Background="White">
<TextBlock x:Name="Message" FontSize="30" />
</Grid>
</UserControl>
|
Listing 2. Recipe 1 app.py Code File
from System.Windows import Application
from System.Windows.Controls import UserControl
class App:
def __init__(self):
root = Application.Current.LoadRootVisual(UserControl(), "app.xaml")
root.Message.Text = "Welcome to Python and Silverlight!"
App()
|
Listing 1 contains the UI XAML file, which looks similar to the typical MainPage.xaml file created as part of a compiled Silverlight application. If you are not familiar with the Python language, Listing 2 may look a bit strange, but you can generally understand that it imports a couple of namespaces with this code:
from System.Windows import Application
from System.Windows.Controls import UserControl
The class declaration is:
class App:
def __init__(self):
root =
Application.Current.LoadRootVisual(UserControl(), "app.xaml")
root.Message.Text = "Welcome to Python and Silverlight!"
This last bit of code creates an instance of the App class:
App()
To compile and run this application, execute this command in the C:\SLDLR\script\SilverlightPythonApp directory:
C:\SLDLR\script\server.bat /b
The server.bat batch command launches Chiron.exe,
which is a command-line utility that creates Silverlight XAP files as
well as enables packageless development of dynamic Silverlight
applications. Chiron creates the output from your simple dynamic
language Silverlight application. Please follow the guidance at these
links to not have to use Chiron:
Creating Interactive Bing Maps with Silverlight and IronRuby
msdn.microsoft.com/en-us/magazine/ee291739.aspx
Back to "Just Text"
ironpython.net/browser/sl-back-to-just-text.pdf
The server.bat batch command also opens the default web browser to http://localhost:2060 and maps the root directory to the directory where the batch command executes, which in this example is C:\SLDLR\script\SilverlightPythonApp, as shown in Figure 1.