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

Creating a Class Library 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 class library. There are Silverlight project templates for C# and for Visual Basic.

This QuickStart explains how to create a solution for a Silverlight-based class library 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 Class Library using Visual Studio

You create a solution for a Silverlight-based class library using the Silverlight Class Library 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 Class Library.

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

    4. Click OK.

  4. The solution is generated.

The solution that is created by the Silverlight Class Library Visual Studio project template will contain a single project for a Silverlight-based class library.

Contents of a Silverlight-Based Application Class Library

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

important

To understand the key elements of a Silverlight-based class library, see Developing a Silverlight Library Assembly.

Configuration

The important configuration details of the generated Silverlight-based class library project includes:

  • 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).

  • 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).

Assembly References

The generated Silverlight-based class library 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.xml.dll

Code Files

The generated Silverlight-based class library project contains the code file for a single class which, by default, is called Class1. 

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.