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

Creating an Application for Silverlight

Introduction

Visual Studio 2008 includes a project template to create a solution with the basic settings and code files for a Silverlight-based managed application. There are Silverlight project templates for C# and for Visual Basic.

This QuickStart explains how to create a solution for a Silverlight-based application using the Visual Studio project template, and explains the default settings and files that are generated.

Prerequisites (available from the Silverlight download site):

  • Silverlight version 2 Beta 1.

  • Visual Studio 2008.

  • Silverlight Tools Beta 1 for Visual Studio 2008.

Creating a Solution for a Silverlight-Based Application using Visual Studio

You create a solution for a Silverlight-based application using the Silverlight Application project template. The following steps outline how.

To create a solution for a Silverlight-based application using Visual Studio

  1. Start Visual Studio 2008.

  2. On the File menu, click New | Project.

  3. In the New Project dialog box

    1. Choose either Visual C# for a C# project, or Other Languages | Visual Basic for a Visual Basic project, and then choose Silverlight.

    2. In the Templates list, choose Silverlight Application.

    3. In the Name text box, enter the desired application name.

    4. Click OK.

  4. In the Add Silverlight Application dialog box, choose a method for hosting your Silverlight-based application:

    1. Choose Add a new Web to the solution for hosting the control if you want to add a separate ASP.NET-based web site to your solution. If you choose this option, you also need to specify the Project Type and Name in the Options group box.

    2. Choose Generate an HTML test page to host Silverlight within this project if you want Visual Studio to add a single HTML page to host your Silverlight-based application to your project.

    3. Click OK.

  5. The solution is generated.

The solution that is created by the Silverlight Application Visual Studio project template will contain:

  • One project for a Silverlight-based application.

  • One web site (only if you chose the Add a new Web to the solution for hosting the control option on the Add Silverlight Application dialog box).

Contents of a Silverlight-Based Application Project

The Silverlight-based application project contains the configuration, assembly references, and code files that are required to build and run a minimal Silverlight-based application.

important

To understand the key elements of a Silverlight-based application, including the application entry point, application assembly, and application package (.xap), see Application Development Overview.

Configuration

The important configuration details of the generated Silverlight-based application project includes the following:

  • Based on the value of the Name text box in the New Project dialog box:

    • The project name.

    • The name of the assembly that is generated when the project is built (see Project | Properties | Silverlight | Application | Assembly name).

    • The default namespace of the generated assembly (see Project | Properties | Silverlight | Application | Default namespace).

    • The name of the Silverlight-based application package (.xap) that is generated when the project is built (see Project | Properties | Silverlight | Application | Silverlight build options | Xap file name).

  • Name and version metadata that is embedded into the generated assembly, and stored in \Properties\AssemblyInfo.cs (you can edit the metadata directly in the AssemblyInfo.cs file, or from Project | Properties | Silverlight | Application | Assembly Information).

  • An application manifest that is required to generate for the application package, but which you should not edit.

Assembly References

The generated Silverlight-based application project will contain references to the following required Silverlight 2 assemblies:

  • mscorlib.dll

  • system.dll

  • system.windows.dll

The following additional references are also included in anticipation of typical application functionality:

  • system.core.dll

  • system.windows.browser.dll

  • system.windows.controls.dll

  • system.windows.controls.extended.dll

  • system.xml.dll

Code Files

The generated Silverlight-based application project contains the code files to build the following classes:

  • Page: The Page class is generated for you to create the main application user interface (UI). Page derives from UserControl and is implemented using a combination of one file for XAML markup (page.xaml) and one file for code-behind (page.xaml.cs for C#, or page.xaml.vb for Visual Basic). For more information on implementing classes using both markup and code-behind, see Code-behind and Partial Classes (Silverlight 2).

  • App: The App class is generated to be the application class, which is required by every Silverlight-based application to start an application running and to show the main application UI. This class is subsequently instantiated by the Silverlight plug-in control directly after the application package (.xap) is downloaded. You specify the application class that handles application startup from Project | Properties | Silverlight | Application | Startup object. App is implemented using a combination of one file for XAML markup (app.xaml) and one file for code-behind (app.xaml.cs for C#, or app.xaml.vb for Visual Basic). For detailed information on the application class, see Developing a Silverlight Application Assem b ly.

Test Page

If you chose the Generate an HTML test page to host Silverlight within this project option on the Add Silverlight Application dialog box, the Silverlight-based application project is configured to create a single HTML page (testpage.html) to host your Silverlight-based application in. The HTML page uses the <OBJECT> tag to configure and instantiate the Silverlight plug-in control to download and run the Silverlight-based application. When the application is run from Visual Studio, the test page is browsed to.

Contents of a Web Site for a Silverlight-Based Application

If you chose the Add a new Web to the solution for hosting the control option on the Add Silverlight Application dialog box, the Silverlight Application project template creates an ASP.NET-based web site and adds it to the generated solution.  The web site includes the typical default ASP.NET web application project files, including:

  • A default web page, implemented using markup (default.aspx) and code-behind (default.aspx.cs or default.aspx.vb).

  • A web site configuration file (web.config).

For more information on building ASP.NET web applications, see ASP.NET Web Applications.

The web site also includes two web pages specifically for hosting the Silverlight-based application, one implemented as an .aspx file and one implemented as an .html file. The name of both files is a concatenation of the name of the Silverlight-based application project and the text, "TestPage". For example, if the name of the Silverlight-based application is "SilverlightApplication1", the generated files would have the following names:

  • SilverlightApplication1TestPage.aspx

  • SilverlightApplication1TestPage.html

The .html file uses the <OBJECT> tag to configure and instantiate the Silverlight plug-in control to download and run the Silverlight-based application. The .aspx file uses the Silverlight ASP.NET server control to generate similar HTML to the .html file (for more information on the Silverlight ASP.NET server control, see Integrating Silverlight with ASP.NET Web Pages).

By default, the .aspx file is configured as the startup web page. When the Silverlight-based application project is built, the application package (.xap) is copied to the web site in order for the .aspx page to be able to download and run it.

Adding More Classes and User Controls

To add more user controls to an existing Silverlight project, use Add | New Item, and choose Silverlight User Control. The generated user control comes from an item template, and will create the XAML markup and code-behind files to implement a custom user control (like the Page class that is generated when you create a new Silverlight-based application project). For more information on implementing classes using both markup and code-behind, see Code-behind and Partial Classes (Silverlight 2). Application's typically use the main user control, Page, as the main application UI that comprises other elements that might include additional user controls. Furthermore, if your application's navigation model requires it, you can "navigate" between "pages" of content by swapping the entire content of the main UI between child user controls.

To add more classes to an existing Silverlight project, use Add | New Item, and choose Class. This simply adds a new empty class to your project.