Application and Programming Models

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Most Silverlight-based applications are written in managed code and make use of the Silverlight application model. However, you have the option of using a simplified programming model instead.

Silverlight provides the following two distinct models for application development:

  • The managed API for Silverlight, which uses code running on the common language runtime (CLR) for Silverlight. You can use the managed API with compiled languages such as Visual Basic and C# or dynamic languages such as IronPython and IronRuby. For more information about dynamic languages, see Silverlight Dynamic Languages SDK.

  • The JavaScript API for Silverlight, which uses JavaScript code interpreted by the browser.

These programming models are distinct because you cannot use them both at the same time within a single instance of the Silverlight plug-in. However, you can implement a splash screen that uses the JavaScript API and then transitions to the managed API when your application has loaded.

The managed API provides significantly more functionality than the JavaScript API, and is the subject of most of the Silverlight documentation. Applications that use the managed API have access to a lightweight version of the .NET Framework. The JavaScript API, however, has access only to the Silverlight presentation core and the browser JavaScript engine.

For more information about the functionality provided by the .NET Framework and the Silverlight presentation core, see Silverlight Architecture.

Managed Programming and the Application Model

The managed API enables you to bundle managed assemblies and resource files into application package (.xap) files. The Silverlight plug-in is responsible for loading an application package and extracting its contents.

An application package must contain an assembly with a class derived from Application. The Application class encapsulates the interaction between your application and the Silverlight plug-in. For example, it enables you to display a user interface within the host Web page. Additionally, it provides application lifetime events and resource management.

The Silverlight documentation uses the term application model to refer to application packaging and the common functionality encapsulated by the Application class. The application model helps you to develop both simple applications and complex, extensible applications that can share resources across a network.

For more information, see Application Model. The application model topics provide guidance on designing applications to enable several common scenarios, such as on-demand resource loading, download progress indication, and navigation.

The Managed API

You can use XAML markup to encapsulate your user interface (UI) designs in both the managed API and the JavaScript API. With the managed API, however, you can factor your application code into multiple XAML files and accompanying code-behind files.

When the Silverlight plug-in loads XAML files, it creates an object tree that your code-behind can manipulate. Your managed application code can also manipulate the HTML Document Object Model (DOM) through the HTML Bridge.

For information about how managed code interacts with XAML and HTML, see Managed API for Silverlight.

The JavaScript API

The JavaScript API is the model that was available in Silverlight 1.0, and is retained for backward compatibility and to enable particular scenarios.

In the JavaScript API, the Silverlight plug-in loads a single XAML page instead of an application package. This XAML can include URI references to server-side resources, such as images and videos. The Silverlight plug-in uses the XAML to create an object tree that you can programmatically manipulate using JavaScript in the host HTML page.

The JavaScript API does not provide an application model that is capable of supporting complex applications with internal navigation. However, it does enable scenarios for which the managed API is too cumbersome, such as splash screens. You can also implement basic navigation by loading new XAML pages into the Silverlight plug-in or by loading new Web pages into the browser.

For more information, see JavaScript API for Silverlight.

JavaScript and the HTML DOM

Both the managed API and JavaScript API enable you to use JavaScript in the host HTML page to integrate Silverlight with the HTML Document Object Model (DOM).

In the JavaScript API, you must implement most of your application functionality using browser-interpreted JavaScript, which can interact directly with the DOM. In the managed API, your JavaScript and DOM code interacts with the Silverlight plug-in and the application model by using the HTML Bridge.

For more information, see Silverlight Programming Models, XAML, and the HTML DOM and HTML Bridge: Interaction Between HTML and Managed Code.

Using Multiple Silverlight Plug-ins in a Web Page

One reason to use the HTML Bridge feature is to create Web pages that integrate Silverlight with other HTML-based content. You can also use the HTML DOM to integrate multiple Silverlight-based applications in a single page. However, in this case, Silverlight provides built-in support for communicating between multiple Silverlight plug-ins, called local messaging.

Local messaging enables you to send messages from one Silverlight-based application to another one running on the same computer. This enables you to create Silverlight content that spans multiple plug-ins, such as an animation that crosses from a banner to a sidebar. For more information, see Communication Between Local Silverlight-Based Applications.

Using Silverlight Outside the Browser

Silverlight provides built-in support for running managed applications outside the browser. After you configure an application for out-of-browser support, users can install it from its host Web page. After installation, users can run the application without a browser or even a network connection. You can configure applications for out-of-browser support without modifying any application code. Alternately, you can implement additional support for out-of-browser mode, such as offline caching and automatic updates. For more information, see Out-of-Browser Support.

In Silverlight 4 and later, you can configure out-of-browser applications to require elevated trust. These trusted applications can bypass some of the restrictions of the Silverlight security sandbox and integrate with native functionality. In Silverlight 5 and later, system administrators can also enable trusted applications to run inside the browser. For more information, see Trusted Applications.

You can also use the Silverlight plug-in in other environments. However, in this case, there is no built-in support. The easiest way to integrate Silverlight with another technology is to host the Silverlight plug-in within a browser control. You can then host the browser control within the other technology. More complex hosting options involve the use of unmanaged code and COM interfaces. If you need the functionality of Silverlight in a desktop application, you should consider using Windows Presentation Foundation (WPF). For more information about using Silverlight in other environments, see Alternative Hosting.

See Also

Other Resources