Continue to the topic of custom templates of Visual Studio (1). We will define a more perfect template. First, we will analyze the composition of a Visual Studio template file. The template exported by Visual Studio 2005 is located"[My documents] \ Visual Studio 2005 \ My Exported Templates \"Folder. To apply the project template to the "Add new project" dialog box, you must copy the template"[My documents] \ Visual Studio 2005 \ Templates \ ItemTemplates".
We can see that a template file is a zip file. decompress the file and we will see three files ,. ico ,. cs ,. vstemplate and ico are the icons displayed in the "Add new project" dialog box ,. the cs file is the original template file we defined, and the vstemplate file is an xml file that defines the template metadata. for Visual Studio 2005, the schema file of this file is saved in"[Visual Studio InstallDirectory] \ Xml \ Schemas \ 2052 \ vstemplate. xsd"The following is a prototype of the file,
<VSTemplate Version = "2.0.0" xmlns = "http://schemas.microsoft.com/developer/vstemplate/2005" Type = "Item">
<TemplateData>
<DefaultName> MyClassTemplate. cs </DefaultName>
<Name> MyClassTemplate </Name>
<Description> & lt; no Description available & gt; </Description>
<ProjectType> CSharp </ProjectType>
<SortOrder> 10 </SortOrder>
<Icon >__ TemplateIcon. ico </Icon>
</TemplateData>
<TemplateContent>
<References/>
<ProjectItem SubType = "Code" TargetFileName = "$ fileinputname $. cs" ReplaceParameters = "true"> MyClassTemplate1.cs </ProjectItem>
</TemplateContent>
</VSTemplate>
For the description of each node, you can refer to the schema file mentioned above, or msdn (ms-help: // MS. VSCC. v90/MS. MSDNQTR. v90.en/dv_vsxmlref/html/templates). There is a subnode <CustomParameters> under the <TemplateContent> node. Under this node, we can easily define our own template parameters, as shown below,
<TemplateContent>
...
<CustomParameters>
<CustomParameter Name="$MyParameter1$" Value="MyValue1"/>
</CustomParameters>
...
</TemplateContent>
We have defined a parameter $ MyParameter1 $. The value of this parameter is MyValue1. The custom template parameters are the same as the default template parameters provided by, we can directly use the template in the original file, as shown below:
#region Copyright (C) Rainsoft All rights reserved
/*******************************************************************************************
* Creation:
* Author: $username$
* Date: $time$
* Description: $MyParameter1$
* Version:
* Modification:
* Author:
* Date:
* Description:
* Version:
*******************************************************************************************/
#endregion
namespace $rootnamespace$
{
using System;
public class $itemname$
{
}
}
We used the template parameter $ MyParameter1 $ that we just defined in Description. VS automatically defines the template parameter as the corresponding value when generating a new item based on the template, however, this value can only be a static value and cannot be dynamically generated. For example, the username used to log on to windows in many companies may be the employee ID of the company, which is not suitable for the Author item in the definition of the partner file header, you can solve this problem by defining the template parameters. However, this method does not work for custom Date items. In the next article, I will provide the final solution.