The first Visual Studio Project template (created manually)

Source: Internet
Author: User
Tags visual studio 2010

Now, we will create a project template manually and study the deployment and use problems. Here we still use the template source prepared in the previous article.

1. Identify the template metadata file.

For details about the format of the template metadata file, refer to the two links:
Visual Studio Template Metadata Files: http://msdn.microsoft.com/en-us/library/xsxc3ete.aspx
Visual Studio Template Schema Reference: http://msdn.microsoft.com/en-us/library/xwkxbww4.aspx

The Schema of the template is still very complex, so here we will only introduce the common nodes involved in our example. By the way, the template metadata also has its own XSD verification file, the default location is as follows:
C: \ Program Files \ Microsoft Visual Studio 10.0 \ xml \ Schemas \ 1033 \ vstemplate. xsd

If you use Visual Studio to open this XSD, You can intuitively see that it defines the format requirements of metadata in detail.

2. One of the best practices for creating a multi-project template manually.

First, the sample structure in my organization is an Optimized Solution and can be considered as one of the best practices, as shown in.

We use. the sln file is organized and used as our project file. In fact, the ZIP package of the Project template is not required. sln file. Here I use it to manage all elements in the template. Of course, we also have a small disadvantage that is not a disadvantage. We will discuss it later.

Of course, what we see here is not the actual physical structure. For details about the physical file structure, refer:

After the logical structure is organized, you can write the. vstemplate file to concatenate all aspects of the project template content.

TwoLayerTemplate. vstemplate code:

<?xml version="1.0" encoding="utf-8"?>
<VSTemplate xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Version="3.0.0" Type="ProjectGroup">
  <TemplateData>
    <Name>Two Layer Web Application</Name>
    <Description>Provide a two layer solution for web development</Description>
    <ProjectType>CSharp</ProjectType>
    <Icon>TwoLayerTemplateIcon.png</Icon>
    <PreviewImage>TwoLayerTemplatePreview.png</PreviewImage>
    <DefaultName>TwoLayerWebApp</DefaultName>
  </TemplateData>
  <TemplateContent>
    <ProjectCollection>
      <ProjectTemplateLink ProjectName="WebClient">WebClient\WebClient.vstemplate</ProjectTemplateLink>
      <ProjectTemplateLink ProjectName="DataService">DataService\DataService.vstemplate</ProjectTemplateLink>
    </ProjectCollection>
  </TemplateContent>
</VSTemplate>

 

WebClient. vstemplate code:

<?xml version="1.0" encoding="utf-8"?>
<VSTemplate xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Version="3.0.0" Type="Project">
  <TemplateData>
    <Name>WebClient</Name>
    <ProjectType>CSharp</ProjectType>
  </TemplateData>
  <TemplateContent>
    <Project File="WebClient.csproj">
      <ProjectItem>Properties\AssemblyInfo.cs</ProjectItem>
      <ProjectItem>Default.aspx</ProjectItem>
      <ProjectItem>Default.aspx.cs</ProjectItem>
      <ProjectItem>Default.aspx.designer.cs</ProjectItem>
      <ProjectItem>Web.config</ProjectItem>
    </Project>
  </TemplateContent>
</VSTemplate>

 

DataService. vstemplate code:

<?xml version="1.0" encoding="utf-8"?>
<VSTemplate xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Version="3.0.0" Type="Project">
  <TemplateData>
    <Name>DataService</Name>
    <ProjectType>CSharp</ProjectType>
  </TemplateData>
  <TemplateContent>
    <Project File="DataService.csproj">
      <ProjectItem>Properties\AssemblyInfo.cs</ProjectItem>
      <ProjectItem>DataService.cs</ProjectItem>
    </Project>
  </TemplateContent>
</VSTemplate>

 

Each vstemplate metadata file contains one <VSTemplate> root node and two subnodes: <TemplateData> and <TemplateContent>.

1) The VSTemplate root node has two attributes:
Version attribute: used to specify the template Version. According to the MSDN reference, VS2010 uses 3.0.0;
Type attribute: Specifies the target Type of the template. Three values are commonly used: Item, Project, and ProjectGroup. Item indicates an Item template, Project indicates a single Project template, and ProjectGroup indicates a multi-Project template.

2) The TemplateData node is used to organize the template information and provide the metadata displayed in the "New Project" dialog box. The key points of the display can be seen in the previous article, in fact, most of the content is similar to the information filled in by the Project Wizard, but more can be done here, for example, the DefaultName attribute, which can be used as the default value of the name in the new project dialog box after being specified, in fact, the [DefaultName] + N format is displayed by default, for example:

3) The main TemplateContent node is used to organize the template source. Here we have to explain the relationship between "TwoLayerTemplate. vstemplate" and "WebClient. vstemplate", "DataService. vstemplate.

Here, I personally define a new concept "root template Metadata File", which indicates that this metadata structure is special and is used to index each individual project "template Metadata File ", I personally give it a new concept "sub-template Metadata File", where "TwoLayerTemplate. vstemplate is the root file. As a subnode of TemplateContent, ProjectCollection is used to store the collection of ProjectTemplateLink. As the name suggests, ProjectTemplateLink is just a link to the metadata files of the other two subtemplates. A multi-project template can contain actual sub-projects or even projects of different programming languages. However, a single multi-project template can only have one general category, which is specified by the ProjectType attribute under the TemplateData file of the root template metadata file.

The ProjectName attribute of ProjectTemplateLink should be mentioned. The final project name of a single project template is specified in the "Create Project" dialog box, but each sub-project of a multi-project template, the final project name cannot be specified one by one, so this attribute is directly used here.

In addition, for the other two "sub-template metadata Files", their configuration methods under the TemplateContent node are the same, and the Project node references. the csproj file and the ProjectItem subnode are used to index all the source files cited in this project. The only thing to note is that the relative paths must be correct.

At this point, you can use the following figure to represent a logical structure of the content related to this project template.

At this time, we are one step away from the conceptual project template. Packaging is actually very simple. Select the content to be packaged and send it to the compressed folder. Here I will use TwoLayerTemplate. the sln file is excluded. This is the "small drawback" that I mentioned in step 1, because this file is not required for the project template. Of course, if it is mistakenly inserted into the package, it can also work.

3. deployment and use results.

The project type defined in the "root template Metadata File" in the "multi-project template" is CSharp, so we need to put it in the following directory during deployment:
C: \ Users \ <CurrentUser> \ Documents \ Visual Studio 2010 \ Templates \ ProjectTemplates \ Visual C #

Otherwise, our template cannot be found in the category table of the "new project" dialog box. Let's take a look at the effect:

You can view the namespace file structure to verify the applicability of the template. Of course, here is a very simple example. The applicable Project template may be very complicated and the customization level will be higher. In the next article, we will continue our discussion.

 

From: http://www.ethan-woo.com/post/2011/04/24/First-VisualStudio-Template-Manually.aspx

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.