简体   繁体   English

Visual Studio:如何将代码模板添加到项目

[英]Visual Studio: How to add code templates to project

I would like to create a feature that allows me to automatically add a few related files to my C# project. 我想创建一个功能,使我可以自动向C#项目中添加一些相关文件。 I want to create a new form based on a template, and create an XML file and add it to a separate sub-directory within the project. 我想基于模板创建一个新表单,并创建一个XML文件并将其添加到项目中的单独子目录中。 How would I go about doing this? 我将如何去做呢? I see that there are wizards, add-ins, macros, etc. available in Visual Studio 2008, but I do not know which is most appropriate for my needs. 我看到Visual Studio 2008中提供了向导,加载项,宏等,但是我不知道哪个最适合我的需求。 I thought a wizard would work, but that may be overkill. 我以为向导会起作用,但这可能会过大。 I just want to remove the repetitiveness of copying from an existing set of files within the same project. 我只想删除同一项目中一组现有文件的重复性。

You can export individual project items as zipped templates using the menu File|Export Template... in VS2010. 您可以使用VS2010中的文件|导出模板...将单个项目项目导出为压缩模板。 To create a template with multiple files, you can export the first item, then add additional items to the resulting template zip. 要创建包含多个文件的模板,您可以导出第一个项目,然后将其他项目添加到生成的模板zip中。 You'll also need to edit the .vstemplate file in the zip to include the additional items. 您还需要编辑zip中的.vstemplate文件,以包括其他项。

As an example, I exported a form from a existing project and then added an xml file. 例如,我从现有项目中导出了一个表单,然后添加了一个xml文件。 The resulting zip file contains the files 生成的zip文件包含以下文件

__TemplateIcon.ico __TemplateIcon.ico
Form1.cs Form1.cs的
Form1.Designer.cs Form1.Designer.cs
Form1.resx Form1.resx
MyTemplate.vstemplate MyTemplate.vstemplate
xmldata.xml xmldata.xml

The .vstemplate file contains the following xml: .vstemplate文件包含以下xml:

<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Item">
  <TemplateData>
    <DefaultName>TwoFileTemplate</DefaultName>
    <Name>TwoFileTemplate</Name>
    <Description>Test template</Description>
    <ProjectType>CSharp</ProjectType>
    <SortOrder>10</SortOrder>
    <Icon>__TemplateIcon.ico</Icon>
  </TemplateData>
  <TemplateContent>
    <References />
    <ProjectItem SubType="" TargetFileName="xml\$fileinputname$.xml" ReplaceParameters="true">xmldata.xml</ProjectItem>
    <ProjectItem SubType="Form" TargetFileName="$fileinputname$.cs" ReplaceParameters="true">Form1.cs</ProjectItem>
    <ProjectItem SubType="" TargetFileName="$fileinputname$.Designer.cs" ReplaceParameters="true">Form1.Designer.cs</ProjectItem>
    <ProjectItem SubType="" TargetFileName="$fileinputname$.resx" ReplaceParameters="true">Form1.resx</ProjectItem>
  </TemplateContent>
</VSTemplate>

Once you've updated the zip file, move it to the folder ...\\Visual Studio 2010\\Templates\\ItemTemplates. 更新zip文件后,将其移动到文件夹... \\ Visual Studio 2010 \\ Templates \\ ItemTemplates。

The new template should now show up in the Add New Item dialog in VS2010. 现在,新模板应显示在VS2010的“添加新项”对话框中。 It will add the form and xml file with whatever name you specify when adding the new item to the project and will put the xml file in a directory named 'xml'. 它将使用您在将新项目添加到项目中时指定的名称添加表单和xml文件,并将xml文件放置在名为“ xml”的目录中。 If you want a fixed name for one or the other of the files you can replace $fileinputnam$ in the .vstemplate file with the desired name. 如果您想要一个或另一个文件的固定名称,则可以使用所需名称替换.vstemplate文件中的$fileinputnam$

Edit - If you want a fixed item name for a form or other class file you'll also need to open the code templates and replace $safeitemname$ with the desired name. 编辑-如果要为表单或其他类文件使用固定的项目名称,则还需要打开代码模板并将$safeitemname$替换$safeitemname$所需的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM