How to create a new Silverlight Project
Silverlight
The Silverlight project file is a text file that you can create and edit using different tools. For example, you can use Visual Studio 2010 and expression blend to create a Silverlight project or modify an existing project. For more information, see the expression blend document.
This topic describes how to use Visual Studio 2010 to create and generate a Silverlight application project or a Silverlight class library project, and how to add a Silverlight library assembly to a project. You need the following components to perform these steps:
This topic contains the following sections:
Silverlight Application Project
Silverlight Class Library Project
Add a Silverlight Library
{
Ca_click ('7096a579-400c-48ca-9c3f-0589eeffda0e ', 'expand', 'folder ')
} "Href =" javascript: void (0) ">
Silverlight Application Project
Use"Silverlight application"Project template to create a Silverlight application project. Follow these steps to create a new Silverlight application project.
Create a new Silverlight Application Project
Start Visual Studio 2010.
In"File"Click"New"And then click"Project".
Will display"New Project"Dialog box.
In"Installed template"In the pane, expand"Visual C #"Or"Visual Basic"Node, and then select"Silverlight".
In the template list, select"Silverlight application". Shows an example of a Silverlight Project template.
Specify the name and location of the application, and then click"OK".
Will display"Create a Silverlight application"Dialog box, as shown in.
Select the method to host the Silverlight application.
If you do not want to use a website to host the Silverlight application, deselect"Host the Silverlight application on the new website"Check box. Then, an HTML test page is generated to host the application.
If you want to add a separate ASP. NET Website or ASP. NET web application project in the solution to host the Silverlight application, select"Host the Silverlight application on the new website"Check box. If you select this option, you must also specify"New web project name"And"New web project type".
Slave"Silverlight version"Select the required Silverlight version from the drop-down list.
Click"OK"Button.
To generate a Silverlight application project, go"Generate"Click"Generate a solution".
To debug a Silverlight application project, go"Debugging"Click"Start debugging".
To run the Silverlight application project, go"Debugging"Click"Start execution (not debugging )".
Silverlight application project file
The Silverlight application project contains the following configuration, Assembly reference, and code files:
Appmanifest. xml
This is the Application List file required to generate the application package. Do not edit this file.
Assemblyinfo. CS or assemblyinfo. VB
This file contains the name and version metadata embedded into the generated assembly.
. Xap File
This is the Silverlight application package. This file is generated when a Silverlight application project is generated. An application package is a compressed zip file with the. xap file extension and contains all the files required to start your application. For more information about the Silverlight application package, see application structure.
The Silverlight application project contains references to the following Assembly:
Mainpage File
You can use the mainpage class to create a user interface for the Silverlight application. The mainpage class is derived from usercontrol. You can implement the mainpage class by using mainpage. XAML (for the XAML tag) and mainpage. XAML. CS or mainpage. XAML. VB (for the code to hide.
APP files
The Silverlight application needs to use the app class to display the application user interface. You can use app. XAML, app. XAML. CS, or app. XAML. VB to implement app classes. After creating an application package (. xap file), The Silverlight plug-in will instantiate the app class.
Test page
If you"Create a Silverlight application"Not selected in the dialog box"Host the Silverlight application on the new website"To generate an HTML page (testpage.html) to host your application.
Silverlight application project website file
If"Create a Silverlight application"Select"Host the Silverlight application on the new website"Check box to create an ASP. NET Website and add it to the Silverlight solution. The website contains the following files:
Silverlight. js
A JavaScript helper file that contains functions used to initialize the Silverlight plug-in instance and functions used to determine the installed plug-in version on the client.
HTML file
The HTML file used to configure and instantiate the Silverlight plug-in. The plug-in downloads and runs the Silverlight application. The file name is connected by the Silverlight application project name and the text "testpage.html.
. Aspx File
The. aspx file that starts the web page by default. The file name is connected by the Silverlight application project name and the text "testpage. aspx.
Web. config
The website configuration file.
For more information about integrating Silverlight into your project, see integrate Silverlight with Web pages.
{
Ca_click ('4ca4ddea-a331-4b7f-ad9d-f6cc991096b6 ', 'expand', 'folder ')
} "Href =" javascript: void (0) ">
Silverlight Class Library Project
Use"Silverlight class library"Project template to create a Silverlight class library project. Follow these steps to create a new Silverlight class library project.
Create a new Silverlight Class Library Project
Start Visual Studio 2010.
In"File"Click"New"And then click"Project".
Will display"New Project"Dialog box.
In"Installed template"In the pane, expand"Visual C #"Or"Visual Basic"Node, and then select"Silverlight".
In the template list, select"Silverlight class library".
Specify the name and location for the class library, and then click"OK".
"Add Silverlight class library"The dialog box appears.
Slave"Silverlight version"Select the required Silverlight version from the drop-down list, and then click"OK".
Silverlight class library project file
The Silverlight class library project contains the following configuration, Assembly reference, and code files:
Assemblyinfo. CS or assemblyinfo. VB
This file contains the name and version metadata embedded into the generated assembly.
The Silverlight class library project contains references to the following Assembly:
Class1. CS orClass1. VB
Code file of a class named class1.
{
Ca_click ('7e356727-b02b-40e4-8f72-2821059e9b2e ', 'expand', 'folder ')
} "Href =" javascript: void (0) ">
Add a Silverlight Library
When you install Silverlight 4 tools for Visual Studio 2010, the Silverlight core runtime assembly and the Silverlight SDK are installed. The Silverlight SDK includes the Silverlight library assembly, which is organized as the client and server library. For a list of controls in the Silverlight SDK, see controls by function category.
The following steps describe how to add a reference to the Silverlight library assembly.
Add reference to the Silverlight library assembly
In"Solution resource Browser", Right-click the Silverlight project, and select"Add reference".
Will display"Add reference"Dialog box.
In". Net"Tab, select the reference to add.
For example, you will select system. Windows. Controls. Data to use the DataGrid Control in the project.
Click"OK".
The reference you added is displayed in the reference node.
Note: |
To exclude the referenced assembly from the application package, select the Assembly reference and"Attribute"In the window"Copy local"Set to false. This method is useful if you want to retrieve an assembly as needed. For more information, see the topic of the application structure. |
To use an assembly in XAML, you must specify the XML namespace ing.
Add XML namespace ing
Open mainpage. XAML.
In the <usercontrol> start tag, add a tag to declare the namespace. For example, you will add the following tag to use the DataGrid.
Copyxmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
To use an element in XAML, add the name specified in the XML namespace ing as the prefix for the name. For example, the DataGrid element in XAML looks like the following:
Copy<sdk:DataGrid></sdk:DataGrid>
For a list of default prefixes and mappings, see the prefixes and Mappings of the Silverlight library.