|
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Features .NET Feature — Creating Templates for Visual Studio 2005
Easy authoring, single file distribution, and file copy installation
By: Xin Yan; Stephen Rakonza
Dec. 18, 2006 03:45 PM
Visual Studio project templates and item templates are reusable and customizable stubs that can simplify the development process. They provide pre-defined starting points for the project or the project items, thus removing the need to create new projects and items from scratch every time.
Visual Studio 2005 installs a number of project templates and item templates to your computer by default. When creating a new project, a developer will have several categories of projects to choose from, as shown in Figure 1. For example, the more common Windows projects include Windows Application and Class Library. These are called Project Templates. A project template contains the files and settings necessary to create a project, such as name and description for this project template, assembly references, default project contents, default project properties, and compiler options. In Figure 1, select the Windows Application template under the Windows project type to create a default C# Windows Form project. Once the project is created, project items can be added through the Add New Item dialog, as shown in Figure 2. This dialog box contains available pre-defined project item templates, such as Class and Interface. Each of these represents an Item Template. An item template contains the files and settings necessary to create a project item. In Figure 2, select Windows Form to add a new Form class to the project. Project templates and item templates allow for easy authoring of the project. Take a look at the Windows Form project we just created. The Windows Application project template sets up the required project properties and assembly references for a Windows Form project. The Windows Form item template creates an empty Form class and adds it to the project with the correct set of assembly references. Together, they free users from low level plumbing work and allow users to concentrate on the custom logic, thus accelerating the development process. Project templates and item templates help distribute reusable code. For example, a company can standardize on a specific layout and style of the About Box with company logo and copyright information. The company's About Box item template could be distributed for reuse by teams within the company. It eliminates redundant coding and reduces possibility of visual inconsistencies between the company's products. Project templates and item templates can be created and distributed by any developer. The rest of the article is dedicated to illustrating how to create project templates and item templates.
Export Template from Visual Studio The Export Template wizard supports both project templates and item templates. For project templates, it creates a template based on the existing project. For item templates, it allows the user to select desired items and assembly references to be included in the item template. Upon finishing, the Export Template wizard generates a .zip file as the template. It contains the following files:
<My Documents>\Visual Studio 2005\Templates\ProjectTemplates\ (for project templates) For a project template, the folder in which it resides determines where the template is displayed in the New Project dialog. If the project template file is under the ProjectTemplates\Visual C#\ folder, the template will be displayed under the Visual C# project type node in the New Project dialog. If it's under the ProjectTemplates\Visual C#\Windows\ folder, it will be displayed under the Windows project type node in the New Project dialog. The project template folder structure maps to the folder structure under <Visual Studio install folder>\Common7\IDE\ProjectTemplates\. After copying the .zip file, the template will be acknowledged by Visual Studio 2005 and displayed the next time the New Project or Add New Item dialog is shown.
The .vstemplate File The .vstemplate file schema is the same for both the project templates and the item templates. Using the Export Template wizard on the Windows Form project we just created will generate a .vstemplate file similar to the one in Listing 1. The first section TemplateData contains metadata used in the display of the template in the New Project dialog or the Add New Item dialog. Some common elements are:
Parameter Replacement When creating a template, the Export Template wizard replaces some of the important code with tokens. For example, an item template created from a Form class will have its namespace and class name replaced with parameter $rootnamespace$ and $safeitemname$. They are used as placeholders for the project's namespace and the item's name. When the template is invoked, these parameters will be replaced by the real project's namespace and the item name entered in the Add New Item dialog. By default, Visual Studio 2005 supports a set of reserved template parameters:
Reader Feedback: Page 1 of 1
Latest Cloud Developer Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week
Breaking Cloud Computing News
|
|||||||||||||||||||||||||||||||||||||||||||||||||