MULTIMEDIA

Silverlight Recipes : Creating Silverlight Using Ruby, Python, or JScript

6/25/2012 5:37:36 PM

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.

Figure 1. Recipe 2-11's application running the SilverlightPythonApp

Figure 2 displays the UI when you click index.html.

Figure 2. Recipe 1's SilverlightPythonApp UI
Other  
  •  Olympus Launches OM-D E-M5
  •  Nikon Launched The D800 And D800E
  •  Hands-On Preview: Pentax K-01- Stylish new compact system camera from Pentax
  •  Birds Of Prey (Part 3) - Behind the scenes
  •  Birds Of Prey (Part 2) - Owl in motion, Prey, Little owl, Expression, Goshawk, Perch
  •  Birds Of Prey (Part 1) - Flight, Gaze, Eagle silhouette
  •  Serif PagePlus X6
  •  Oloneo HDRengine
  •  Magix Music Maker MX
  •  Arcsoft Perfect365
  •  Displacement Maps
  •  Astrophotography Alternative : Canon EOS 60Da, Lumix GF5
  •  CyberLink PowerDirector 10 Ultra
  •  Viewsonic VP2365-LED – Go LED, Go Green
  •  Philips Brilliance 241P4QPYES – Not a cheap TN
  •  Corsair Vengeance 1300 Analog Gaming Headset
  •  Where Is My Hard Drive Capacity? (Part 2) - Beat the browser
  •  Where Is My Hard Drive Capacity? (Part 1) - ITunes culprit
  •  Viewsonic V3D231
  •  Sapphire Vid-2X Display Expander
  •  
    Video
    Top 10
    What To Do With An Old Mac (Part 4)
    What To Do With An Old Mac (Part 3)
    What To Do With An Old Mac (Part 2)
    What To Do With An Old Mac (Part 1)
    Technology News: Cloud Computing To Influence Security Offerings
    We Help You Find Your Ideal Smartphone (Part 5)
    We Help You Find Your Ideal Smartphone (Part 4)
    We Help You Find Your Ideal Smartphone (Part 3)
    We Help You Find Your Ideal Smartphone (Part 2)
    We Help You Find Your Ideal Smartphone (Part 1)
    Most View
    Headset & Speaker Buyer’s Guide (Part 1)
    Microsoft Dynamics AX 2009 : Building Lookups - Picking a color
    HomePlug Buyer’s Guide (Part 3) - ZyXEL PLA4205 Gigabit Adaptor & Netgear Powerline AV+ 500 Adapter XAV5501
    Top 10 Camcorders - Jan 2013
    Computing Yourself Fit (Part 2)
    Windows Tips & Tricks (May 2012) - Part 3 : Check the stability of your Windows system, Display particular Wi-Fi keys once again
    Build Up Your Dream House with PC (Part 1)
    Windows Vista Services that You Need Understand
    iPad SDK : Popovers - The Font Name Popover (part 1)
    Meet “The New iPAD” : Retina Display, A5X CPU, iSight Camera
    Managing Exchange Server 2010 : The Exchange Management Shell (part 1) - Bulk user creation in the Exchange Management Shell
    Buying Guide: Gaming PCs For Hardcore Gamers' Dreams (Part 4) - Yoyotech Warbird 3570XTA
    iPhone 3D Programming : Adding Shaders to ModelViewer (part 2)
    Windows 7 : Troubleshooting Common Problems on Small Networks
    Revive A Crashed Notebook
    SteelsSeries Kinzu Pro Edition Mouse - Simplicity
    iPhone, iPod touch and iPad : VSCO CAM
    .NET security : Administering Isolated Storage
    Lomond EvoJet Office
    All The Stuff You Didn't Know (Part 4) - Booting From Backup & Dead Island Is Dead