QUICKSTARTS
[This topic is pre-release documentation and is subject to change in future releases of Microsoft Silverlight.]

Programming Silverlight with Dynamic Languages

Introduction

This QuickStart sample shows how to create a Microsoft Silverlight-based application that uses managed code and a dynamic language. The sample also shows how to debug the application in Visual Studio. The Silverlight Tools Beta 1 for Visual Studio 2008 includes three dynamic languages, IronPython, IronRuby, and Managed JScript.

Run View
Language:

Using dynamic languages in your Silverlight-based application involves the following tasks:

  • Understanding how dynamic languages work in Silverlight-based applications.

  • Accessing class libraries in managed assemblies.

  • Creating a Silverlight-based dynamic language application and running it by using Chiron.exe.

  • Debugging a Silverlight-based dynamic language application with Chiron.exe and Visual Studio.

Prerequisites (available from the Silverlight download site):

  • Silverlight version 2 Beta 1.

  • Visual Studio 2008.

  • Silverlight Tools Beta 1 for Visual Studio 2008.

Programming a Silverlight-Based Application with a Dynamic Language

A simple Silverlight-based application that uses a dynamic language contains the following:

  • The root HTML or .aspx file that is the entry point for a browser.

  • app.xaml, which defines the user interface for your Silverlight-based application.

  • app.py, app.rb, or app.jsx, which contains the dynamic language code that handles events at run time. For dynamic languages, this file is not compiled into an assembly. Dynamic language code is compiled and executed at run time, on the client computer. The dynamic language engine and Dynamic Language Runtime (DLR) support files are downloaded to the client computer as part of the Silverlight installation. You do not need to include them in your project.

The source code for this sample shows how to hook up these elements. You can run the source code in your local browser, by using Chiron.exe.

The Silverlight Tools Beta 1 for Visual Studio 2008 does not include Visual Studio project templates for dynamic languages.

In order to write a Silverlight-based application, you need to access the Silverlight class libraries, plus any additional managed assemblies you want to use. By default, IronPython and Managed JScript provide references to the following assemblies:

  • System.Windows.dll

  • mscorlib.dll

  • System.dll

  • System.Scripting.Silverlight.dll

  • System.Xml.Core.dll

The way you access additional libraries depends on which dynamic language you are using.

To access the Silverlight class libraries from IronPython

  1. Use the import statement to load the clr module.

    import clr
  2. Use the clr.AddReference function to load assemblies, and make their contents available for import.

    clr.AddReference("MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b17a5c561934e089")
  3. Use the import statement to add namespaces to the current context or to load modules. You must import namespaces before you can use them.

    import System.Windows
    import System
    import MyPythonModule
  4. Use the from … import statement to add the contents of a namespace or module to the current context.

    from System.Windows import *

To access the Silverlight class libraries from Managed JScript

  1. Use the AddReference function to load assemblies, and make their contents available for import.

    AddReference("MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b17a5c561934e089")
  2. Use the Import function to import namespaces into the current context. You must import namespaces before you can use them.

    Import("System.Windows")
    Import("System")
    note

    You can import from System without having to add any references, but before you can use a namespace in code, you must first import it.

  3. Use the Import function to import the contents of a namespace into the current context.

    Import("System.Windows.*")
    Import("System.*")
    note

    In addition to cluttering the global context, adding all the types in System replaces the Managed JScript Array with System.Array.

  4. Use the ImportAlias function to import a namespace or type into the current context with an alias.

    SW = ImportAlias("System.Windows")
  5. Use the LoadModule and LoadModuleFromFile functions to load DLR modules. The methods return the loaded module. The second parameter of LoadModuleFromFile specifies the type of the module, "js" or "py".

    util = LoadModule("MyUtilities")
    util = LoadModuleFromFile("MyUtilities", "py")

In the Silverlight Tools Beta 1 for Visual Studio 2008, Managed JScript has the following limitations:

  • The mapping between .NET Framework objects and Managed JScript objects is currently limited. For example, the Managed JScript String object cannot be used in places where System.String is expected, but the string primitive can be used. Managed JScript arrays cannot be used where .NET Framework arrays are expected.

  • There is limited support for Javascript-style enumeration (that is, listing names instead of values); it is supported only for objects that implement IList and IDictionary. Enumeration on other .NET Framework objects lists values instead of names.

  • Generics are not yet supported.

  • Method overload resolution for candidate methods with numeric types other than double is not yet supported.

  • Exceptions thrown in event handlers that are hooked up from Managed JScript code are not logged at the top of the HTML. Exceptions that are hooked up in markup are logged.

  • The Eval function does not produce a correct value unless its input is a sequence of statements ending in a simple expression statement.

  • The arguments.callee property is not supported.

Debugging a Silverlight-Based Application Using Visual Studio

With the Silverlight Tools Beta 1 for Visual Studio 2008, you can use Visual Studio to debug a Silverlight-based application that uses dynamic languages.

Debugging in Visual Studio with Silverlight and Dynamic Languages

Your Silverlight-based application must be downloaded to a browser in order to run. The debugger must be started after the code is downloaded. Each time you run your application for debugging, you must attach the debugger to the browser. To force the browser to load a new copy of your application, instead of using the cached copy, you must close the browser between debugging sessions.

To debug a dynamic language project in Visual Studio

  1. From the root directory of your Silverlight-based application, run Chiron.exe with the /browser (/b) option to create the .xap file and serve the directory in the browser.

  2. In the browser, click Default.aspx to start the application. The application startup code runs.

  3. In Visual Studio, on the Debug menu, click Attach to Process.

    Because your application is running in the browser, you must attach to the browser process in order to debug it.

  4. In the Attach to Process dialog box, in the Available Processes list, in the Process column, click the appropriate instance of the browser to select it.

    If you have other instances of the browser open, giving Default.aspx a title will make it easy to identify in the Title column.

  5. Click Attach.

    A message box appears, informing you that your application is running the Core managed runtime for Silverlight, and asking whether you want to debug this runtime.

  6. Click Yes to debug using the Silverlight runtime.

  7. Switch to the browser and press F5 to refresh Default.html.

    Your application is reloaded under the debugger, and you are now ready to debug. Any breakpoints you have set in your initialization code are hit when your application is reloaded.

    important

    You must refresh the page, to reload your application after the debugger is attached. If you start interacting with your application without first refreshing the browser, none of your breakpoints will be hit.

  8. When you are finished debugging, close the browser. Otherwise, the browser will used its cached copy of your application the next time you debug.

    It is not necessary to close the ASP.NET Development Server between debugging sessions.

By default, unhandled exceptions in the application are logged at the top of the HTML page. You can turn this feature off with the following code.

This does not affect syntax errors, which occur before the code can execute.

Status of Debugging Features

In Microsoft Silverlight 1.1 Alpha, support for debugging applications written in dynamic languages has some limitations. The following list provides the status of some debugging features:

  • Setting breakpoints works with all languages.

  • Stepping works over and into code works with all languages. [note about IPy]

  • Support for Edit and Continue has not been added, so you cannot change code while you are debugging.

  • You can drag the execution pointer back to execute code again, or forward to skip code.

  • In Managed JScript, support for viewing local variables and function parameters has not yet been added. Currently there is no way to view these in watches, quick watches, Locals window, or Immediate window.

  • In IronPython, you can view local variables in the Locals window, the Immediate window, quick watches, and watches. You cannot view function parameters, but you can work around this by assigning function parameters to local variables. You can set variables in the Immediate window.

    In dynamic languages, a variable that has not been set has no type. The type belongs to the value, not to the variable. The debugger shows the value of an unassigned variable as null.

    In the Locals window, you can expand objects contained in local variables, and view their properties.

    In the Immediate window, you can examine and set the properties of objects, but in this release you must cast the objects to a specific type in order for the debugger to recognize their properties and methods. For example, the following shows the use of the Immediate window to set the color of text in a TextBlock. After the property is set, the state of the SolidColorBrush is echoed back to the window.

    ((System.Windows.Media.SolidColorBrush)((System.Windows.Controls.TextBlock)s).Foreground).Color = System.Windows.Media.Colors.Yellow
    {#FFFFFF00}
        A: 255
        B: 0
        G: 255
        R: 255
        ScA: 1.0
        ScB: 0.0
        ScG: 1.0
        ScR: 1.0
Page view counter