You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Visual Studio 2022自定义模板问题:参数不替换且重复显示

自定义Visual Studio模板问题解决方案

问题一:模板参数不生效

问题描述

为批量创建12月的相似代码文件,制作了如下C#模板:

namespace $rootnamespace$
{
    internal class Day$dayNumber$ : DayBase<$outputType$>
    {
        public Day$dayNumber$() : base($dayNumber$) { }

        public override int FirstProblem()
        {
            throw new NotImplementedException();
        }

        public override int SecondProblem()
        {
            throw new NotImplementedException();
        }
    }
}

使用「添加新项」功能时,$dayNumber$、$outputType$等参数未被替换,生成的代码保留了占位符。已在VSTemplate中设置ReplaceParameters="true"。

解决步骤

  1. 添加自定义参数声明
    VS不会自动识别未定义的自定义参数,需在VSTemplate的<TemplateData>与<TemplateContent>之间添加<CustomParameters>节点,声明参数及默认值:
    <CustomParameters>
      <CustomParameter Name="$dayNumber$" Value="1"/>
      <CustomParameter Name="$outputType$" Value="int"/>
    </CustomParameters>
    
  2. 验证模板文件关联
    确保<ProjectItem>节点中指定的源文件名(如Day1.cs)与模板包内的实际文件名一致,且该文件确实包含参数占位符。
  3. 重新打包并替换模板
    修改后将所有模板文件重新打包为.zip,替换原模板文件,重启Visual Studio后测试。

问题二:模板修改后重复显示

问题描述

修改VSTemplate中的Name和Description后,「添加新项」列表中同时出现新旧两个模板,已确认本地模板文件夹及OneDrive中无重复文件。

解决步骤

  1. 清理模板缓存
    • 关闭所有Visual Studio实例
    • 删除以下路径的缓存文件夹:
      • 用户模板缓存:%LOCALAPPDATA%\Microsoft\VisualStudio\<VS版本号>\ItemTemplatesCache
      • 系统模板缓存:%PROGRAMFILES(x86)%\Microsoft Visual Studio\<VS版本号>\<版本类型>\Common7\IDE\ItemTemplatesCache
        (替换<VS版本号>为实际版本,比如17.0;<版本类型>为Community/Professional等)
  2. 强制重新扫描模板
    • 以管理员身份打开命令提示符,切换到VS的IDE目录(示例路径:C:\Program Files (x86)\Microsoft Visual Studio\2022\Community\Common7\IDE)
    • 执行命令:
      devenv /installvstemplates
      
  3. 确认同步状态
    若模板存储在OneDrive目录下,等待同步完成后再重启VS,避免旧版本文件残留。

内容的提问来源于stack exchange,提问作者Ari Roth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 05:59:57