Visual Studio 2012项目模板自定义参数和重命名问题

5

任务:
我想从Visual Studio 2012项目创建一个项目模板,以便可以快速创建类似的项目。这需要重命名文件和更改根名称空间。不幸的是,这并没有发生...

已完成的工作:

  1. 我创建了该项目,并按照MSDN页面上的说明替换了某些名称。一个替换是$safeprojectname$,另一个是自定义参数:$custom1$
  2. 通过文件 --> 导出模板...选项将项目导出为项目模板。
  3. 我解压了创建的zip文件。
  4. 我编辑了.vstemplate.csproject文件。代码如下所示。
  5. 我重新压缩了文件。
  6. 重新启动了Visual Studio,并打开了一个项目。
  7. 我选择添加 --> 新建项目 --> *我的自定义模板*,给它命名并添加该项目。
  8. 项目被添加并替换了$safeprojectname$。文件没有被重命名,$custom1$也没有被替换。

MyTemplate.vstemplate

<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Project">
   <TemplateData>
      <Name>MyTemplateName</Name>
      <Description>My Description</Description>
      <ProjectType>CSharp</ProjectType>
      <ProjectSubType></ProjectSubType>
      <SortOrder>1000</SortOrder>
      <CreateNewFolder>true</CreateNewFolder>
      <DefaultName>My Default Name</DefaultName>
      <ProvideDefaultName>true</ProvideDefaultName>
      <LocationField>Enabled</LocationField>
      <EnableLocationBrowseButton>true</EnableLocationBrowseButton>
      <Icon>__TemplateIcon.ico</Icon>
   </TemplateData>
   <TemplateContent>
      <Project TargetFileName="MyTemplate.csproj" File="MyTemplate.csproj" ReplaceParameters="true">
         <ProjectItem ReplaceParameters="true" TargetFileName="app.config">app.config</ProjectItem>
         <ProjectItem ReplaceParameters="true" TargetFileName="$safeprojectname$Data.cs">Data.cs</ProjectItem>
         <ProjectItem ReplaceParameters="true" TargetFileName="$safeprojectname$.cs">Main.cs</ProjectItem>
         <ProjectItem ReplaceParameters="true" TargetFileName="$safeprojectname$Form.xaml">Form.xaml</ProjectItem>
         <ProjectItem ReplaceParameters="true" TargetFileName="$safeprojectname$Form.xaml.cs">Form.xaml.cs</ProjectItem>
         <Folder Name="Properties" TargetFolderName="Properties">
            <ProjectItem ReplaceParameters="true" TargetFileName="AssemblyInfo.cs">AssemblyInfo.cs</ProjectItem>
            <ProjectItem ReplaceParameters="true" TargetFileName="Settings.settings">Settings.settings</ProjectItem>
            <ProjectItem ReplaceParameters="true" TargetFileName="Settings.Designer.cs">Settings.Designer.cs</ProjectItem>
         </Folder>
      </Project>
      <CustomParameters>
         <CustomParameter Name="$custom1$" Value="Some.Custom.Namespace.Prefix."/>
      <CustomParameters>
   </TemplateContent>
</VSTemplate>

MyTemplate.csproj

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
    <PropertyGroup>
        ...
    <OutputType>Library</OutputType>
    <RootNamespace>$custom1$$safeprojectname$</RootNamespace>
    <AssemblyName>$safeprojectname$</AssemblyName>
    </PropertyGroup>
    <ItemGroup>
        <Compile Include="$safeprojectname$.cs" />
        <Compile Include="$safeprojectname$Data.cs" />
        <Compile Include="$safeprojectname$Form.xaml.cs">
            <DependentUpon>$safeprojectname$Form.xaml</DependentUpon>
        </Compile>
        <Compile Include="Properties\AssemblyInfo.cs" />
        <Compile Include="Properties\Settings.Designer.cs">
            <AutoGen>True</AutoGen>
            <DesignTimeSharedInput>True</DesignTimeSharedInput>
            <DependentUpon>Settings.settings</DependentUpon>
        </Compile>
    </ItemGroup>
    <ItemGroup>
        <Page Include="$safeprojectname$Form.xaml">
            <SubType>Designer</SubType>
            <Generator>MSBuild:Compile</Generator>
        </Page>
    </ItemGroup>
    <ItemGroup>
        <None Include="app.config" />
        <None Include="Properties\Settings.settings">
            <Generator>PublicSettingsSingleFileGenerator</Generator>
            <LastGenOutput>Settings.Designer.cs</LastGenOutput>
        </None>
    </ItemGroup>
</Project>

Question?
How do I get Visual Studio to change the $custom1$ parameter in the files and to rename the files, like Main.cs, to $safeprojectname$.cs?

What doesn't work

  • Following the steps on the MSDN pages got me nowhere; neither the filenames change, nor the custom parameter.
  • Searching Google gave me MSDN pages, or references to them.

2个回答

0

你的步骤和项目文件看起来是正确的。你需要更新 Visual Studio 模板缓存。如何更新 Visual Studio 模板缓存 这个答案链接到了步骤。

以下是对我有效的步骤(请注意,你的用户名将不同):

  1. 关闭所有打开的Visual Studio实例。
  2. 编辑位于C:\Users\chadd\Documents\Visual Studio 2013\My Exported Templates的模板副本 - 按照上面OP中的步骤进行。
  3. 搜索以查找其他模板副本并将其删除。我使用的是Visual Studio 2013,在这两个目录中找到了副本。C:\Users\chadd\AppData\Roaming\Microsoft\VisualStudio\12.0\ProjectTemplatesCacheC:\Users\chadd\Documents\Visual Studio 2013\Templates\ProjectTemplates
  4. 将编辑后的yourTemplate.zip版本复制到C:\Users\chadd\Documents\Visual Studio 2013\Templates\ProjectTemplates中。
  5. 为Visual Studio打开命令提示符(以管理员身份运行),然后输入devenv /installvstemplates。此步骤需要一段时间(几分钟),请耐心等待命令提示符返回。
  6. 打开Visual Studio,验证您修改后的模板现在是否正常工作。

0

我不知道Visual Studio如何处理导出的模板,但我知道从修改默认模板中,Visual Studio有两个版本的模板。一个在“ProjectTemplate”文件夹中,另一个在“ProjectTemplateCache”中。

也许您导出的模板仍然在缓存中没有进行修改。尝试通过使用参数“devenv /installvstemplates”或“devenv /setup”从cmd启动Visual Studio来刷新缓存。

如果您想创建多个模板,我建议下载Visual Studio并使用VSIX项目部署模板。SDK还允许以更高效的方式调试模板。您可以按下Debug按钮,将收到一个安装了您的VSIX(即您的模板)的Visual Studio实例。


1
糟糕...看起来我要么在MSDN上读过一些信息,要么MSDN不够具体。当导出模板时打开的文件夹并不是正在加载的模板。相反,您需要转到..\Templates\文件夹。我将将此答案标记为答案,因为它指向了我正确的方向。谢谢。 - Knots
Knots,你的帖子帮助我弄清楚了如何重命名项目中的文件名。你知道如何替换项目的实际名称吗? - Harry Boy
你指的是哪个名称?项目的显示名称还是创建项目时的默认名称? - Sukram
Sukram模板创建的项目名称。 - Harry Boy
如果我使用你上面提供的MyTemplate.vstemplate示例,我尝试根据用户定义的值更改项目名称,像这样:<Project TargetFileName="$customvalue".csproj" File="MyTemplate.csproj" ReplaceParameters="true">但它不起作用。 - Harry Boy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接