QUICKSTARTS
[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 with the Silverlight Tools Beta 2 for Visual Studio 2008 installed includes a project template to create a managed application based on Silverlight version 2. There are Silverlight project templates for C# and for Visual Basic. The solution that gets created includes the basic settings and code files for a Silverlight-based application.

This QuickStart explains how to create a solution for a Silverlight-based application by 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 2.

  • Visual Studio 2008.

  • Silverlight Tools Beta 2 for Visual Studio 2008.

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

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

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

  1. Start Visual Studio 2008.

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

    The New Project dialog box opens.

  3. In the Project types pane, expand the Visual C# or Visual Basic node and then select the Silverlight project type.

  4. In the Templates list, select Silverlight Application.

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

  6. Click OK.

    The Add Silverlight Application dialog box opens.

  7. Select a method for hosting your Silverlight-based application.

    • Select 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 select this option, you also must specify the Project Type and Name in the Options group box.

    • Select Dynamically 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.

  8. Click OK.

    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 select Add a new Web to the solution for hosting the control in 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 include the following:

  • Names that are 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 in C# or Project | Properties | Silverlight | Application | Root namespace in VB).

    • 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 AssemblyInfo.cs or AssemblyInfo.vb (you can edit the metadata directly in the AssemblyInfo file, or from Project | Properties | Silverlight | Application | Assembly Information).

  • An application manifest that is required to generate 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.Net.dll

  • System.Windows.Browser.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 by 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 about 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. It is required by every Silverlight-based application to start an application and to show the main application UI. This class is subsequently instantiated by the Silverlight plug-in 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 by 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 more information about the application class, see Developing a Silverlight Application Assembly.

Test Page

If you select Dynamically generate an HTML test page to host Silverlight within this project in the Add Silverlight Application dialog box, the Silverlight-based application project is configured to create a single HTML page (TestPage.html) in which to host your Silverlight-based application. This HTML page uses the HTML object element to configure and instantiate the Silverlight plug-in, which downloads and runs your Silverlight-based application. When you run your application within Visual Studio, the test page opens in your Web browser.

Contents of a Web Site for a Silverlight-Based Application

If you select Add a new Web to the solution for hosting the control in 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 by using markup (Default.aspx) and code-behind (Default.aspx.cs or Default.aspx.vb).

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

For more information about 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 element to configure and instantiate the Silverlight plug-in, which downloads and runs your Silverlight-based application. The .aspx file uses the Silverlight ASP.NET server control to generate similar HTML to the .html file. For more information about 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 so that the .aspx page can download and run it.

Adding More User Controls and Classes

To add more user controls to an existing Silverlight project, use Add | New Item, and select 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 about how to implement classes using both markup and code-behind, see Code-behind and Partial Classes (Silverlight 2). Applications typically use the main user control, Page, as the main application UI. The main user control consists of 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 select Class. This just adds a new empty class to your project.

Page view counter