Visual Studio 2013中用VSIX创建项目模版

我会一步一步解释:

  1. 我的一个项目里有5个工程, 我想用其中的4个去创建一个新的工具模版,我还想把他们都放到一个文件夹下面当我用这个模版创建新的工程时.注意我的初始化项目,我想把他们转换成一个模版(我不会用个"InstallerCreator"这个工程所以可以忽略它).
    Step1
  2. 在vs中, go to File -> Export Template ... 然后会弹出一个对话框.
    Step2
  3. 默认的"Project Template" 选择按钮会被选上,如果不是,请选上. 在下拉列表中,选择一个我们想导出为模版的工程
  4. Step3
  1. 下一步的窗口中,写下你的模版介绍,你可以选择一个你想要的图标. 取消选择"Automatically import the template into visual studio"的配置,你也可以取消选择另外一个按钮,如果你不想马上看到你导出的模版目录, 然后选择Finish.
  2. Step4
  1. 就这样我导出了四个工程当作模版, 如下图所示, 在我的电脑上文件夹是"C:UsersASUSDocumentsVisual Studio 2012My Exported Templates"
    Step5
  2. 现在打开VS中的File > New > Project, 然后弹出新的工程窗口
    Step6
  3. 选择 Extensibility > VSIX Project, 把工程名命名为“MVCStarterKitInstaller” 然后点击OK.
    Step7
  4. 在这个解决方案中增加另一个工程, 这个应该是一个C#类型的模版, 命名为"MVCStartKit" 然后点击OK                                                                                              vsix_step8
  5. 解压出了在第五步的所有4个工程模版的zip文件, 他们放到了 “MVCStarterKit”工程文件夹下.
    Step9
  6. 更名解压出来的模版中.vstemplate的后缀的所有文件, 例如, 每个压缩包解压后默认名字为: "MyTemplate.vstemplate", 你应该把他们重命名为和工程相关的名字,如: "Domain.vstemplate".
    Step10_c
  7. 如果你想在模版中增加任何nuget的包, 你需要在vstemplate的导出文件里面加上下面这段, 然后再解压.
    Step10_b
  8. <WizardExtension>
        <Assembly>NuGet.VisualStudio.Interop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</Assembly>
        <FullClassName>NuGet.VisualStudio.TemplateWizard</FullClassName>
      </WizardExtension>
      <WizardData>
        <packagesrepository="extension"
              repositoryId="MVCStarterKitInstaller.Proggasoft.3ca090ba-cf48-48cb-be9b-7c406a371af1">
          <packageid="jQuery"version="2.0.0"/>
          <packageid="jQuery.UI.Combined"version="1.8.20.1"/>
          <packageid="jQuery.Validation"version="1.9.0.1"/>
          <packageid="Microsoft.jQuery.Unobtrusive.Validation"version="2.0.30506.0"/>
          <packageid="Microsoft.Web.Infrastructure"version="1.0.0.0"/>
          <packageid="Modernizr"version="2.6.2"/>
        </packages>
      </WizardData>
  9. 确保所有解压出来的文件下面的文件的Build Action设置为Content.                                                                                                                                             Step9_b
  10. 现在你只需要在"MVCStarterKit"中添加一个叫做"MVCStarterKit.vstemplate"的文件, 并加上对应的icon图标文件: __TemplateIcon.ico. 模版文件内容如下:
    Step9_c
     1 <VSTemplateVersion="2.0.0"Type="ProjectGroup"
     2     xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
     3   <TemplateData>
     4     <Name>Asp.net MVC 3 Starter Kit</Name>
     5     <Description>A template for Asp.net MVC 3 projects</Description>
     6     <Icon>__TemplateIcon.ico</Icon>
     7     <ProjectType>CSharp</ProjectType>
     8   </TemplateData>
     9   <TemplateContent>
    10     <ProjectCollection>
    11       <SolutionFolderName="Domains">
    12         <ProjectTemplateLinkProjectName="Domain">
    13           DomainDomain.vstemplate
    14         </ProjectTemplateLink>
    15         <ProjectTemplateLinkProjectName="Domain.Test">
    16           Domain.TestDomainTest.vstemplate
    17         </ProjectTemplateLink>
    18       </SolutionFolder>
    19       <SolutionFolderName="Web">
    20         <ProjectTemplateLinkProjectName="Web">
    21           WebWeb.vstemplate
    22         </ProjectTemplateLink>
    23     <ProjectTemplateLinkProjectName="Web.Tests">
    24           Web.TestsWebTests.vstemplate
    25         </ProjectTemplateLink>
    26       </SolutionFolder>
    27     </ProjectCollection>
    28   </TemplateContent>
    29 </VSTemplate>
  11. Please check in the “MVCStarterKit.vstemplate” that  “Domain” and “Domain.Test” is instructed to be inside “Domains” solution folder and “Web” and “Web.Tests” is instructed to be inside the “Web” solution folder when we use the starter kit to create a new project.
    Step10
  12. Add reference of “MVCStarterKit” project in “MVCStarterKitInstaller” project and double click “source.extension.vsixmanifest”
    Step11
  13. Type “Product name”, “Description”, “Author” in Metadata screen.
    Step12
  14. In “Install targets” leave it as it is.
    Step13
  15. In “Assets” tab, select Project template from drop down, then select “A project in this solution” from drop down and then select “MVCStarterKit” in drop down.
    Step14
  16. In “Dependencies” leave it as it is.
    Step15
  17. Now build the solution in the output you will see the path of the VSIX file.
    Step16
  18. Go to that folder and you will find the VSIX file.
    Step17
  19. Double click the file and press “Install”.
    Step18
  20. Now if you go to create a new project, you will see our created template in the project templates.
    Step19
  21. If you want to uninstall the template, go to : Tools > Extension and updates.
    Step20
  22. Press Uninstall and it will be uninstalled.
    Step21
原文地址:https://www.cnblogs.com/ZengYunChun/p/5741626.html