[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 with the Silverlight Tools Beta 2 for Visual Studio 2008 installed includes a project template to create a managed class library 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 class library.
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):
Creating a Solution for a Silverlight-Based Class Library using Visual Studio
You create a solution for a Silverlight-based class library by using the Silverlight Class Library project template. The following steps outline how.
To create a solution for a Silverlight-based class library using Visual Studio
-
Start Visual Studio 2008.
-
On the File menu, click New | Project.
The New Project dialog box opens.
-
In the Project types pane, expand the Visual C# or Visual Basic node and then select the Silverlight project type.
-
In the Templates list, select Silverlight Class Library.
-
In the Name text box, enter the desired class library name.
-
Click OK.
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.
Configuration
The important configuration details of the generated Silverlight-based class library 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).
-
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).
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:
Code Files
The generated Silverlight-based class library project contains the code file for a single class named Class1.
Adding More Classes and 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.
To add 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.