Powered by

US - English
NEW! Silverlight 5 is available Learn More

Dynamic Languages in Silverlight

By Microsoft Silverlight Team|July 18, 2011|Level 300 : Intermediate

In addition to C# and VB.NET, Silverlight applications can also be written in programming languages built using the Dynamic Language Runtime (DLR), like Ruby and Python.

Logos

Get Started

Writing Silverlight applications in Ruby, Python, and other DLR-based languages only requires a local web server, a text editor, and a browser which supports Silverlight on Windows or Mac OS, or Moonlight on Linux systems. With either Silverlight or Moonlight installed, the simplest DLR-based application can be contained in a single HTML file:


<script src="http://gestalt.ironruby.net/dlr-latest.js"
        type="text/javascript"></script>
<script type="text/ruby">
    window.alert "Ruby in the browser!"
</script>
<script type="text/python">
    window.Alert("Python works too!")
</script>

Use your favorite text editor to write code, and then just refresh the browser to see your changes. You can also write code in a browser session for a truly interactive experience:

This allows you to quickly write HTML or Silverlight graphics based web applications, in any development environment, with powerful scripting languages:

The DLR enables its languages to be embedded in any Silverlight application through a language-neutral hosting API (even if written with C#, VB, or any language supporting Silverlight), allowing Silverlight applications to be extensible through scripts, whether it's for internal use or targeted at your application's users. For example, here is Seesmic Desktop 2 (a Silverlight 4 application running out of the browser) with an IronPython interactive console in the application’s UI:

See IronRuby and IronPython's browser pages for more information and documentation. Also see the DLR Hosting spec and related documents for more information about how dynamic languages are used from other .NET based languages.

Silverlight enables the Dynamic Language Runtime to bring language choice to the modern browsers, and in turn gives Silverlight developers an interactive environment for making them more productive. Install Silverlight from Silverlight.net’s Get Started page. Only the Silverlight runtime is required, as you don't need Visual Studio installed to develop DLR-based Silverlight applications.

Example Applications

To see firsthand how dynamic languages in Silverlight work, here are some example applications using IronPython and IronRuby:

Speaking Mushroom

Speaking MushroomView | Download

Very basic “Hello, World” style application to show the simplicity of a DLR-based Silverlight application

Mandelbrot

MandelbrotView | Download

A Python application, using C# code to do the heavy lifting of computing the Mandelbrot set (requires Silverlight 3 or greater).

Webcam

WebcamView | Download

DLR-languages can also use the Silverlight APIs directly; this sample shows IronPython directly using the webcam features in Silverlight (requires Silverlight 4 or greater).

DLRConsole

DLRConsoleView | Download

DLRConsole is written in Python, and provides a console for Python and Ruby, allowing you to see how your code affects the on-screen canvas. A quick walk-through of DLRConsole can be found on Jimmy Schementi’s blog. Note that any application has access to a similar console for experimenting in your own application; see the documentation for more info.

Photoviewer

PhotoviewerView | Download

Photoviewer is a Ruby application that searches Flickr for images and displays them using Lightbox. It illustrates how Ruby and JavaScript can work together, and also how to create a HTML-only Silverlight application. A walk-through of this example can be found on Jimmy Schementi’s blog.

Embedding and Testing

Embeding and TestingDownload

This example shows how to use dynamic language from a language built into Silverlight, C#. This application is a calculator which can run Python functions, and whose unit tests run in the browser with Ruby. The Testing in Silverlight with Ruby and Scripting Silverlight with Python walk-throughs show in detail how this works, especially how to easily test your Silverlight applications. You can find the full set of example applications on IronRuby and IronPython's examples page.

More resources

Use these resources to learn more about how dynamic languages can help you be more productive in Silverlight.

Walkthroughs, Talks, and Podcasts

  • Python in the browser – Jimmy Schementi talks about Python in the browser, enriching Python’s feature-set with the ability to write browser apps with the .NET framework.
  • Ruby in the browser – A online walk-through of how Ruby in the browser works, and what you can do with it.
  • Pumping Iron on the Web – Jimmy Schementi talks about dynamic languages on the web, including Silverlight support as well as a prototype of Windows Phone 7 Series support.

Jimmy Schementi’s blog is a great resource for dynamic languages and Silverlight content, as he's the main contributor to Silverlight and the DLR integration. He is also a developer on the IronRuby and DLR projects, and wrote Gestalt, a collaboration between the DLR team MixOnline Labs to add support for "DLR-based languages in HTML script tags" to the already existing Silverlight integration for DLR languages, which uses the XAP Silverlight application model.

Documentation

All documentation for using Dynamic Languages in Silverlight can be found on IronRuby and IronPython’s documentation pages.

Contribute

Both IronRuby, IronPython, and the Dynamic Language Runtime are open-source, licensed under the Microsoft Public License, an OSI approved open-source license. Both projects welcome contributions to samples, documentation, and libraries. IronRuby accepts code contributions to its implementation of the Ruby standard library as well. Visit the IronRuby and IronPython CodePlex sites for more information on contributing.

Microsoft Silverlight Team

By Microsoft Silverlight Team, Silverlight is a powerful development platform for creating engaging, interactive user experiences for Web, desktop, and mobile applications when online or offline.

Comments (0)