Printer Friendly Version      Send     
Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Silverlight
 Dynamic Languages in Silverlight 2
Silverlight 2
Dynamic Languages in Silverlight 2
[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

The Silverlight Tools Beta 2 for Visual Studio 2008 includes the dynamic language runtime and three dynamic languages: IronPython, IronRuby, and Managed JScript. Visual Studio 2008 project templates for dynamic languages are not yet available, but you can work with dynamic languages using the Chiron.exe tool.

The dynamic language runtime and the three dynamic languages are currently under development on the Codeplex Web siteSilverlight samples. You can find more information there, including source code and Silverlight samples.

This topic shows Xaml, HTML, and application code in all three dynamic languages for a simple Silverlight-based application.

See Programming Silverlight with Dynamic LanguagesSilverlight 2 QuickStart, a Silverlight 2 QuickStart that includes short procedures for creating Silverlight-based applications and debugging them with Visual Studio 2008, along with sample code that you can view and run for all three dynamic languages. 

The simplest structure for a Silverlight-based application that uses dynamic languages consists of two folders, app and assets, plus an HTML file.

  • The app folder contains the dynamic language code, app.py, app.jsx, or app.rb, plus an app.xaml file.

  • The assets folder includes a js folder, which contains silverlight.js, and any other assets, such as bitmaps, that your application uses.

Chiron.exe uses the index.html file to serve your application. The default location for this file is the root folder that contains your app and assets folders. The same file can be used with all three dynamic languages. Similarly, the same app.xaml file can be used with all languages.

Your application need not be limited to this simple folder structure. This is only a basic structure that Chiron.exe can use without specifying any special parameters. Your application program can access arbitrarily complex folder structures, and can contain any number of dynamic language code files.

The app File

The dynamic language code in the app.* file is the entry point for your application. To load the root visual element, your dynamic language code first uses the static Application..::.Current property to get the current Application object. For dynamic languages, this is an instance of the DynamicApplication class. Use the DynamicApplication..::.LoadRootVisual method to load the root element and assign it to the Application..::.RootVisual property. In the simple example described here, the root is the app.xaml file described later in this topic.

Simple samples of this code are provided here for all three languages. In each case, the entry point code is defined, and then the entry point function is called. The code is compiled dynamically when you run the application.

See the Programming Silverlight with Dynamic Languages QuickStart for sample code that shows how to handle events in XAML.

IronPython (app.py)

from System.Windows import Application
from System.Windows.Controls import UserControl

class App:
    def __init__(self):
        self.scene = Application.Current.LoadRootVisual(UserControl(), "app.xaml")

    def start(self):
        # TODO: replace this with your application start logic
        self.scene.Message.Text = "Welcome to Silverlight and IronPython."

App().start()

IronRuby (app.rb)

include System::Windows
include System::Windows::Controls

class App

  def initialize
    @scene = Application.Current.LoadRootVisual(UserControl.new(), "app.xaml")
  end

  def start
    # TODO: replace this with your application start logic
    @scene.find_name('Message').text = "Welcome to Silverlight and IronRuby."
  end
end

App.new.start

Managed Jscript (app.js or app.jsx)

Import("System.Windows.Application")
Import("System.Windows.Controls.UserControl")

function App() {
    this.scene = Application.Current.LoadRootVisual(new UserControl(), "app.xaml")
}

App.prototype.start = function() {
    // TODO: put application logic here
    this.scene.Message.Text = "Welcome to Silverlight and Managed JScript."
}

app = new App
app.start()

Using Chiron.exe to Run Your Application

Once you have set up the basic folder structure and files, use the Visual Studio 2008 command prompt to run Chiron.exe. To prepare for using Chiron.exe, add its location to the Path environment variable for your system. Chiron.exe is located in the Tools folder of your Silverlight Tools Beta 2 for Visual Studio 2008 installation.

Run Chiron.exe from the root folder of your application. By default, Chiron.exe looks in the app folder for the app.* dynamic language file and the app.xaml file. These files define the entry point for your application. Chiron.exe includes all the files from the app folder (and any folders it contains) in the .xap file, along with the files from the assets folder, the dynamic language runtime assemblies, and the assembly for the dynamic language your application uses.

Note:

You can use more than one dynamic language in an application . However , each dynamic language you add increases the size of the . xap file.

The simplest way to run Chiron.exe is to let it generate the .xap file on the fly. To do this, run Chiron with the /browser (/b) option as follows: Chiron /b. Chiron builds the .xap file and starts the browser, serving the root folder. Click on the HTML page in the browser to run your application.

Note:

While you are working with your application in the browser, Chiron continues to run in the command window, displaying status messages. When you have finished working with your application and closed the browser tab or window, press C TRL +C to stop Chiron.exe.

Each time you make changes in your application code, Xaml, or HTML, you must refresh the browser page. Chiron.exe intercepts the refresh request and serves a new .xap file containing your edited files. The Programming Silverlight with Dynamic Languages QuickStart provides a simple demonstration of this iterative development technique.

Chiron.exe has options to compile your .xap file for deployment, to specify other locations for the app.* dynamic language application file, to generate a manifest file, and so on. Chiron.exe has command-line help that lists these options, or you can find them in the Readme.txt file located with Chiron.exe. The Readme.txt file also includes information about configuring Chiron.exe to work with other dynamic languages.

Debugging in Visual Studio

To debug your application, open the dynamic language files you want to debug in Visual Studio 2008 and set breakpoints. You do not need a project file or a solution file.

Use Chiron.exe to run your application. When your application is running in the browser, use the Debug menu in Visual Studio 2008 to attach to the browser process. Giving your application a distinctive page title makes it easier to find the correct process.

Refresh the browser page to begin debugging. Breakpoints are not live until the page is refreshed. You can restart the application any time you want by refreshing the page.

You can use F10 and F11 to step over or into code. Use the Locals window to examine variable values, and the Immediate window to set variable values or execute code. Current levels of support for debugging tools vary by language.

Note:

In IronPython , w hen you step off the end of a function with F10 and control returns to the browser , Visual Studio 2008 displays a dialog box informing you that there is no source code available. Press OK to close the dialog box, and then press F5 to continue.

You must make changes to your application code, Xaml, and HTML outside Visual Studio. When you refresh the browser page, Chiron.exe intercepts the request and serves a new .xap file, and Visual Studio 2008 prompts you to reload the files that were changed. The Programming Silverlight with Dynamic Languages QuickStart demonstrates this iterative technique for developing and debugging your dynamic language application.

App.xaml

The following app.xaml file can be used with the application code for any of the dynamic languages:

<UserControl
  xmlns="http://schemas.microsoft.com/client/2007"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="System.Windows.Controls.UserControl"
  x:Name="Page"
  >
  
  <TextBlock 
    x:Name="Message" TextWrapping="Wrap" Foreground="Black" >
  </TextBlock>
</UserControl>

Default.html

The Default.html file provided here can be used with any of the dynamic languages. The file demonstrates the use of a <div> to display formatted error messages. The following HTML fragment shows how initParams is used to specify that errors are reported to the <div> whose id is errorLocation:

    <object data="data:application/x-silverlight," type="application/x-silverlight-2-b2" width="100%" height="100%">
      ...
      <param name="initParams" value="debug=true,reportErrors=errorLocation" />
      ...
    </object>

The DynamicApplication..::.ReportUnhandledErrors property can be used to set the error reporting location from your dynamic language code. The HTML fragment also sets the initial value of the DynamicApplication..::.Debug property.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html  >

<head>
  <title>Dynamic Silverlight Test Page </title>

  <style type="text/css">
    html, body {
      height: 100%;
      overflow: auto;
    }
    body {
      padding: 0;
      margin: 0;
    }
    #silverlightControlHost {
      height: 100%;
    }
  </style>

  <!-- Formatting for DLR error handling -->
  <link type="text/css" rel="stylesheet" href="assets/stylesheets/error.css" />

  <!-- 
    Error handling for when DLR errors are disabled (with 
    reportErrors=false, or not defined at all)
  -->
  <script type="text/javascript">
    function onSilverlightError(sender, args) {
      if (args.errorType == "InitializeError")  {
        var errorDiv = document.getElementById("errorLocation");
        if (errorDiv != null)
          errorDiv.innerHTML = args.errorType + "- " + args.errorMessage;
      }
    }
  </script>
</head>

<body>
  <!-- 
    Syntax/Runtime errors from Silverlight will be displayed here.
  This will contain debugging information and should be removed 
    or hidden when debugging is completed 
  -->
<div id='errorLocation' style="font-size: small;color: Gray;"></div>

  <div id="silverlightControlHost">
    
    <object data="data:application/x-silverlight," type="application/x-silverlight-2-b2" width="100%" height="100%">
      <param name="source" value="app.xap"/>
      <param name="onerror" value="onSilverlightError" />
      <param name="background" value="white" />
      <param name="initParams" value="debug=true,reportErrors=errorLocation" />
      <param name="windowless" value="true" />
      
      <a href="http://go.microsoft.com/fwlink/?LinkID=108182" style="text-decoration: none;">
          <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>
      </a>
    </object>
    <iframe style='visibility:hidden;height:0;width:0;border:0px'></iframe>
     
  </div>

</body>
</html>
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker