Export (0) Print
Expand All

Developing a Silverlight Application Assembly

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

This topic covers the building blocks that form the foundation of every Silverlight application. These building blocks include the application class, application assembly, application package, and entry point metadata.

This topic contains the following sections.

Note:

this document covers how to develop a Silverlight application class, assembly, and package using MSBuild. For Visual Studio development of Silverlight applications, see The Silverlight Preview Window.

The one class that every Silverlight application must implement is the application class. The application class initializes and starts the application. To save you the effort, Silverlight encapsulates initialization and startup logic in the Application class. Consequently, you create the application class by deriving a public class from Application.

The following example shows an implementation of an application class using code:

using System.Windows; // Application
namespace SilverlightApplication
{
    public class App : Application 
    {
        // Default constructor
        public App() { }
    }
}


In this example, the application class is implemented as the public App class, in the SilverlightApplication namespace, which derives from Application.

In Silverlight, the most common technique for implementing an application class is to use a combination of markup (using a XAML file) and code-behind (using a code file). Using markup and code-behind can reduce how much code you have to write, if not avoid it altogether.

The following example shows the App class from the previous example re-implemented by using markup and code-behind.

<Application 
    xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    x:Class="SilverlightApplication.App">

</Application>


using System.Windows; // Application

namespace SilverlightApplication
{
    public partial class App : Application
    {
        public App()
        {
            // Initialize custom properties and events.

            // Merge application class markup implementation with 
            // this object.
            InitializeComponent();
        }
    }
}


The markup file is written in XAML, and specifies the XAML and Silverlight namespaces respectively. For more information about namespaces in Silverlight, see XAML and Mapping Custom XML Namespace Values (Silverlight 2).

The code-behind file is written in C# (although it could be any managed programming language supported by Silverlight). The combination of the x:Class attribute, the partial keyword, and the InitializeComponent method are required to associate the markup and code-behind files. For more information about development using markup and code-behind, see Code-behind and Partial Classes (Silverlight 2).

The application class must be compiled into an application assembly before it can be added to the application package. To build the application assembly with the application class requires MSBuild, which is a tool for building applications. You provide MSBuild with a project file, which is an XML-based file that tells MSBuild what to build, which compiler to build it, and where to build it to.

Note:

For more information about MSBuild, see MSBuild Reference.

To build the application assembly requires an MSBuild project file that has a configuration such as the following:

<!-- SilverlightApplication.proj -->
<Project 
  ToolsVersion="3.5"
  DefaultTargets="Build" 
  xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <!-- Application Configuration -->
  <PropertyGroup>

    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
    <SchemaVersion>2.0</SchemaVersion>
    <NoStdLib>true</NoStdLib>
    <NoStdCfg>true</NoStdCfg>

    <!--
        Build a library assembly, SilverlightApplication.dll, with 
        SilverlightApplication as the root namespace. Place the assembly
        in the ClientBin folder.
    -->
    <RootNamespace>SilverlightApplication</RootNamespace>
    <AssemblyName>SilverlightApplication</AssemblyName>
    <OutputType>Library</OutputType>
    <OutputPath>ClientBin</OutputPath>

  </PropertyGroup>

  <!-- Silverlight assembly references required by code -->
  <ItemGroup>
    <Reference Include="mscorlib" />
    <Reference Include="system" />
    <Reference Include="System.Windows" />
  </ItemGroup>

  <!-- Files to build application class -->
  <ItemGroup>
    <Compile Include="App.xaml.cs">
      <DependentUpon>App.xaml</DependentUpon>
    </Compile>
    <ApplicationDefinition Include="App.xaml">
      <Generator>MSBuild:MarkupCompilePass1</Generator>
    </ApplicationDefinition>
  </ItemGroup>

  <!-- 
    The file that is used by MSBuild to Build C# Silverlight Applications, and
    which specifies the C# compiler. Note that $(MSBuildExtensionsPath) is the 
    path to the Program Files\MSBuild folder.
  -->
  <Import Project="$(MSBuildExtensionsPath)\Microsoft\Silverlight\v2.0\Microsoft.Silverlight.CSharp.targets" />
</Project>

The key configuration details of this MSBuild project file include the following:

  • The OutputType and OutputPath properties that specify to generate a managed library assembly (SilverlightApplication.dll) in the ClientBin folder.

  • References to the three required Silverlight assemblies: Mscorlib (mscorlib.dll), System (System.dll), and System.Windows (System.Windows.dll).

  • References to the code files to build the application class.

  • An Import property that specifies the file (Microsoft.Silverlight.CSharp.targets) which implements the logic for building Silverlight applications.

From the command-line, you call msbuild.exe and specify the MSBuild project file that it should use to build your application. The following example shows the command-line for building the SilverlightApplication project:

msbuild.exe SilverlightApplication.proj

After MSBuild builds the project, the following files are generated in the ClientBin folder:

  • SilverlightApplication.dll (the application assembly).

  • SilverlightApplication.pdb (the debug file for the application assembly).

After building the application assembly, it should subsequently be included in an application package.

Silverlight projects for MSBuild can specify additional properties to embed the application assembly in an application package when projects are built. The following MSBuild project file shows the additional configuration that is required to create the application assembly:

<!-- SilverlightApplication.proj -->
<Project ... >

  <!-- Application Configuration -->
  <PropertyGroup>
    ...
    <OutputPath>ClientBin</OutputPath>
    ...
    <!-- 
        Generate the application package (.xap) that includes the 
        application assembly and application class.
     -->
    <XapOutputs>true</XapOutputs>
    <XapFilename>SilverlightApplication.xap</XapFilename>

  </PropertyGroup>
  ...
</Project>

After MSBuild generates the application assembly, SilverlightApplication.dll, MSBuild packages it into the application package (SilverlightApplication.xap) according to the values of the XapOutputs and XapFilename properties. When MSBuild completes, the following files are generated in the ClientBin folder:

  • SilverlightApplication.dll (the application assembly).

  • SilverlightApplication.pdb (the debug file for the application assembly).

  • SilverlightApplication.xap (the application package).

Even though the application assembly, with application class, is now embedded in the application package, additional work must be performed to allow the Silverlight plug-in to find the application class and instantiate it.

The Silverlight plug-in’s job is twofold. First, it starts the common language runtime (CLR) and creates an application domain for the Silverlight application to run in. Second, the Silverlight plug-in downloads the application package, works with the Silverlight runtime to create an execution environment for the Silverlight application, and loads the assemblies and resource files included in the application package. The next step is to instantiate the application class to start the application running. However, in order to instantiate the application class, the Silverlight runtime must know about the application entry point, which is both the application assembly and the class in the application assembly that is the application class. The Silverlight runtime looks for entry point metadata that provides these details, which you add to the application package using additional MSBuild project properties, as shown in the following MSBuild project:

<!-- SilverlightApplication.proj -->
<Project ... >

  <!-- Application Configuration -->
  <PropertyGroup>
    ...
    <!-- 
      Set the metadata used by the Silverlight run time to find the 
      application assembly and the application class within it.
    -->
    <SilverlightApplication>true</SilverlightApplication>
    <SilverlightAppEntry>SilverlightApplication.App</SilverlightAppEntry>
    <SilverlightManifestTemplate>AppManifest.xml</SilverlightManifestTemplate>
    <GenerateSilverlightManifest>true</GenerateSilverlightManifest>

  </PropertyGroup>
  ...
</Project>

SilverlightApplication specifies that the assembly which MSBuild will generate is the application assembly and SilverlightAppEntry indicates which class is the application class.

GenerateSilverlightManifest indicates that MSBuild must generate a manifest file and include it in the application package. This manifest file, named AppManifest.xaml, includes the specified application and entry point information.

SilverlightManifestTemplate indicates which file to use as a template for the manifest. The specified template file must be located in the same folder as the project file, and will typically have the same name as the generated manifest: AppManifest.xaml. The manifest template must contain, at least, an empty Deployment element.

Once you have implemented your application class, compiled it into an application assembly, embedded the application assembly into an application package, and furnished the application package with the entry point metadata, you can download and run your application. The following Web page includes the minimum HTML and script that you must have to use the Silverlight plug-in to download and run a Silverlight application:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<!-- saved from url=(0014)about:internet -->
<head>
  <style type="text/css">
    html, body { height: 100%; overflow: auto; }
    body { padding: 0; margin: 0; }
    #silverlightControlHost { height: 100%; }
  </style>  
</head>
<body>
  <object 
    data="data:application/x-silverlight,"
    type="application/x-silverlight"
    width="100%" height="100%">
    <param name="source" value="SilverlightApplication.xap"/>
    <a 
      href="http://go2.microsoft.com/fwlink/?LinkID=115261"
      style="text-decoration: none;">
      <img 
          src="http://go2.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>
</body>
</html>

For more information about instantiating a Silverlight plug-in, by using either the <OBJECT> tag or silverlight.js, see Instantiating a Silverlight Plug-In (Silverlight 2).

Show:
© 2014 Microsoft