C# Visual Studio 2012项目模板自定义参数&;重命名问题

C# Visual Studio 2012项目模板自定义参数&;重命名问题,c#,visual-studio-2012,visual-studio-templates,C#,Visual Studio 2012,Visual Studio Templates,任务: 我想从Visual Studio 2012项目创建一个项目模板,以便快速创建类似的项目。这需要重命名文件并更改根命名空间。不幸的是,这并没有发生。。。 所做的工作: 我已经创建了项目,并替换了MSDN页面指定的某些名称。一个替换项是$safeprojectname$,另一个替换项是自定义参数:$custom1$ 项目已通过文件-->导出模板导出为项目模板。。。选择权 我已经解压缩了创建的zip文件 我已经编辑了.vstemplate和.csproject文件。代码如下所示 我已经重新

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

所做的工作:
  • 我已经创建了项目,并替换了MSDN页面指定的某些名称。一个替换项是
    $safeprojectname$
    ,另一个替换项是自定义参数:
    $custom1$
  • 项目已通过文件-->导出模板导出为项目模板。。。选择权
  • 我已经解压缩了创建的zip文件
  • 我已经编辑了.vstemplate和.csproject文件。代码如下所示
  • 我已经重新压缩了文件
  • Visual Studio已重新启动,并且已打开一个项目
  • 我选择添加-->新项目-->*我的自定义模板*,给它一个名称并添加项目
  • 将添加项目并替换
    $safeprojectname$
    。不会重命名文件,也不会替换
    $custom1
  • MyTemplate.vstemplate

    
    MyTemplateName
    我的描述
    CSharp
    1000
    真的
    我的默认名称
    真的
    启用
    真的
    __TemplateIcon.ico
    app.config
    Data.cs
    Main.cs
    Form.xaml
    Form.xaml.cs
    AssemblyInfo.cs
    设置。设置
    Settings.Designer.cs
    
    MyTemplate.csproj

    
    ...
    图书馆
    $custom1$$safeprojectname$
    $safeprojectname$
    $safeprojectname$Form.xaml
    真的
    真的
    设置。设置
    设计师
    MSBuild:编译
    PublicSettingsSingleFileGenerator
    Settings.Designer.cs
    
    问题?
    如何让Visual Studio更改文件中的$custom1$参数并将文件(如Main.cs)重命名为$safeprojectname$.cs

    什么不起作用

    • 遵循MSDN页面上的步骤对我毫无帮助;文件名不变,自定义参数也不变
    • 搜索谷歌给了我MSDN页面,或者对它们的引用

    我不知道visual stuido handels是如何导出模板的,但通过修改默认模板,我知道visual studio有两个版本的模板。一个在“ProjectTemplate”文件夹中,另一个在“ProjectTemplateCache”中

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


    如果要创建多个模板,建议下载VisualStudio并使用VSIX项目来存储这些模板。SDK还允许以更有效的方式调试模板。您可以按Debug,然后通过安装VSIX(模板)接收Visual Studio实例。

    您的步骤和项目文件看起来是正确的。您需要更新Visual Studio模板缓存。步骤链接中的答案

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

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

  • 哦。。。看来我已经阅读了MSDN的一些信息,或者MSDN不够具体。导出模板时打开的文件夹不是正在加载的模板。相反,您需要转到..\Templates\文件夹。我将把这个答案标记为答案,因为它为我指明了正确的方向。谢谢。Knots,你的帖子帮助我找到了如何重命名项目中的文件名。你知道如何替换项目的实际名称吗???你的意思是什么?项目的显示名称或创建项目时的默认名称?由模板Sukram创建的项目名称。如果我以上面的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>
    
    <?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>