Compartir a través de


How to: Create a New Silverlight Project

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Silverlight project files are text files that you can create and edit using different tools. For example, you can use Visual Studio 2010 and also Expression Blend to create Silverlight projects or modify existing projects. For more information, see the Expression Blend documentation.

This topic describes how to use Visual Studio 2010 to create and build a new Silverlight application project or a Silverlight class library project. It also describes how to add a Silverlight library assembly to your project. You need the following components to perform these steps:

  • Silverlight 4 or Silverlight 5.

  • Silverlight Tools for Visual Studio 2010.

  • Visual Studio 2010.

This topic contains the following sections:

  • Silverlight Application Project

  • Silverlight Class Library Project

  • Adding a Silverlight Library Assembly

Silverlight Application Project

A Silverlight application project is created by using the Silverlight Application project template. Use the following steps to create a new Silverlight application project.

To create a new Silverlight application project

  1. Start Visual Studio 2010.

  2. On the File menu, click New and then Project.

    The New Project dialog box appears.

  3. In the Installed Templates pane, expand the Visual C# or the Visual Basic node and select Silverlight.

  4. In the list of templates, select Silverlight Application. The following illustration shows an example of the Silverlight project templates.

    Silverlight Project Templates

  5. Specify a name and a location for the application and then click OK.

    The New Silverlight Application dialog box appears as shown in the following illustration.

    New Silverlight Application dialog box

  6. Select a method for hosting your Silverlight application.

    1. Uncheck the Host the Silverlight application in a new Web site check box if you do not want to use a Web site to host your Silverlight application. Instead, an HTML test page will be generated to host your application.

    2. Check the Host the Silverlight application in a new Web site check box if you want to add a separate ASP.NET Web site or ASP.NET Web Application Project to your solution to host the Silverlight application. If you select this option, you must also specify the New Web project name and New Web Project Type.

  7. From the Silverlight Version drop-down list, select the version of Silverlight that you want to target.

  8. Click the OK button.

  9. To build the Silverlight application project, in the Build menu, click Build Solution.

  10. To debug the Silverlight application project, in the Debug menu, click Start Debugging.

  11. To run the Silverlight application project, in the Debug menu, click Start Without Debugging.

NoteNote:

Silverlight applications must be built for the Any CPU platform. If you target the x64 platform in Configuration Manager, you will receive errors when you build. Targeting x64 in Configuration Manager is not a supported scenario.

Silverlight Application Project Files

A Silverlight application project contains the following configuration, assembly references, and code files:

  • AppManifest.xml

    This is the application manifest file that is required to generate the application package. You must not edit this file.

  • AssemblyInfo.cs or AssemblyInfo.vb

    This file contains the name and version metadata that is embedded into the generated assembly.

  • .xap file

    This is the Silverlight application package. It is generated when the Silverlight application project is built. An application package is a compressed zip file that has a .xap file extension and contains all the files that you need to start your application. For more information about a Silverlight application package, see Application Structure.

  • A Silverlight application project contains references to the following assemblies:

    • mscorlib.dll

    • System.dll

    • System.Core.dll

    • System.Net.dll

    • System.Windows.dll

    • System.Windows.Browser.dll

    • System.Xml.dll

  • MainPage files

    The MainPage class is used to create the user interface for the Silverlight application. The MainPage class derives from UserControl. You can implement the MainPage class by using MainPage.xaml for XAML markup and MainPage.xaml.cs or MainPage.xaml.vb for the code-behind.

  • App files

    The App class is required by a Silverlight application to display the application user interface. The App class is implemented by using App.xaml and App.xaml.cs or App.xaml.vb. The App class is instantiated by the Silverlight plug-in after the application package (.xap file) is created.

  • Test Page

    A single HTML page (TestPage.html) is generated to host your application if you do not check the Host the Silverlight application in a new Web site check box in the New Silverlight Application dialog box.

Silverlight Application Project Web Site Files

If you check the Host the Silverlight application in a new Web site check box in the New Silverlight Application dialog box, an ASP.NET Web site is created and added to the Silverlight solution. The Web site contains the following files:

  • Silverlight.js

    A JavaScript helper file that contains functions to initialize Silverlight plug-in instances and functions for determining the client's installed version of the plug-in.

  • HTML file

    An HTML file that is used to configure and instantiate the Silverlight plug-in, which downloads and runs the Silverlight application. The name of this file is a concatenation of the name of the Silverlight application project and the text "TestPage.html".

  • .aspx file

    A .aspx file that is the default startup Web page. The name of this file is a concatenation of the name of the Silverlight application project and the text "TestPage.aspx".

  • Web.config

    A Web site configuration file.

For more information about integrating Silverlight into your projects, see Integrating Silverlight with a Web Page.

Silverlight Class Library Project

A Silverlight class library project is created by using the Silverlight Class Library project template. Use the following steps to create a new Silverlight class library project.

To create a new Silverlight class library project

  1. Start Visual Studio 2010.

  2. On the File menu, click New and then Project.

    The New Project dialog box appears.

  3. In the Installed Templates pane, expand the Visual C# or the Visual Basic node and select Silverlight.

  4. In the list of templates, select Silverlight Class Library.

  5. Specify a name and a location for the class library and then click OK.

    The Add Silverlight Class Library dialog box appears.

  6. From the Silverlight Version drop-down list, select the version of Silverlight that you want to target and click OK.

Silverlight Class Library Project Files

The Silverlight class library project contains the following configuration, assembly references, and code files:

  • AssemblyInfo.cs or AssemblyInfo.vb

    This file contains the name and version metadata that is embedded into the generated assembly.

  • A Silverlight class library project contains references to the following assemblies:

    • mscorlib.dll

    • System.dll

    • System.Core.dll

    • System.Net.dll

    • System.Windows.dll

    • System.Windows.Browser.dll

    • System.Xml.dll

  • Class1.cs or Class1.vb

    The code file for a single class named Class1.

Adding a Silverlight Library Assembly

When you install the Silverlight Tools for Visual Studio 2010, the Silverlight core runtime assemblies are installed, as well as, the Silverlight SDK. The Silverlight SDK includes the Silverlight library assemblies, which are organized into client and server libraries. For a list of the controls that are in the Silverlight SDK, see Controls by Function.

The following steps show how you can add a reference to a Silverlight library assembly.

To add a reference to a Silverlight library assembly

  1. In Solution Explorer, right-click your Silverlight project and then select Add Reference.

  2. The Add Reference dialog box appears.

  3. In the .NET tab, select the reference you want to add.

  4. For example, you would select System.Windows.Controls.Data to use the DataGrid control in your project.

  5. Click OK.

  6. The reference you added appears in the References node.

NoteNote:

To exclude the referenced assembly from the application package, select the assembly reference and then, in the Properties window, set Copy Local to False. This is useful when you want to retrieve the assembly on demand. For more information see the Application Structure topic.

To use the assembly in XAML, you have to specify an XML namespace mapping.

To add an XML namespace mapping

  1. Open MainPage.xaml.

  2. In the <UserControl> start tag, add markup to declare a namespace. For example, you would add the following markup to use the DataGrid.

    xmlns:sdk="https://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
    
  3. To use the element in XAML, prefix the name with the name specified in the XML namespace mapping. For example, the DataGrid element in XAML looks like the following.

    <sdk:DataGrid></sdk:DataGrid>
    

    For a list of the default prefixes and mappings, see Prefixes and Mappings for Silverlight Libraries.