【问题标题】:Visual Studio item template: Replace with project nameVisual Studio 项模板:替换为项目名称
【发布时间】:2014-11-09 19:16:22
【问题描述】:

我正在尝试在 Visual Studio 2013 中为 EF Code-First 上下文类创建项目模板。除了我想要一个指向当前项目中命名空间的 using 语句之外,我已经完成了所有工作。例如,当在 ProjectName.Contexts 中创建模板项的实例时, using 语句将是:

使用项目名称.Models

问题是我找不到替换项目模板中的部件的方法。据我所知,$projectname$ 和 $safeprojectname$ 仅适用于项目模板,而不适用于项目模板。

我的模板如下:

using System;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using $safeprojectname$.DAL.Models; // This is the problem line

namespace $rootnamespace$
{
    public class $safeitemrootname$
        : DbContext
    {
        // Constructors =======================================================
        public $safeitemrootname$()
            : base("$safeitemrootname$")
        {
        }

        // DBSets =============================================================
        // public DbSet<Company> Companies { get; set; }

        // Configuration ======================================================
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }
}

更新:

作为对 Hans 评论的回应,这是我尝试使用 $projectname$ 时的输出($safeprojectname$ 也是如此):

using System;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using $projectname$.DAL.Models; // This is the problem line

如您所见,$projectname$ 替换只是被忽略了。

任何帮助将不胜感激,

杰森

【问题讨论】:

  • 不,这在项目模板中也可以正常工作。它被项目的默认命名空间值替换。在项目 + 属性,应用程序选项卡中设置。

标签: c# visual-studio templates


【解决方案1】:

Root.vstemplate 中添加copyParameters="true",如下所示

 ProjectTemplateLink ProjectName="Domain.$projectname$" CopyParameters="true">

在代码中使用$ext_projectname$解析项目名称

using Provider.$ext_projectname$

【讨论】:

  • 这适用于(VS2017)基于压缩文件的多项目模板,但 ProjectName="Domain.$projectname$" 也是项​​目文件夹的名称以及 Domain.$projectname$.csproj文件也一样,项目自己的 vstemplate 文件的 TargetFileName="MyNewName.csproj" 无效。
【解决方案2】:

如下面的 MSDN 链接所述:

https://msdn.microsoft.com/en-us/library/eehb4faa.aspx

只有在新建项目对话框中提到项目名称时才会出现项目名称。因此,如果您在现有项目中使用它,它将无法正常工作。请看下面的截图:

【讨论】:

    【解决方案3】:

    我的同事找到了一个参数来修复它,它检测到当前的项目名称。

    $rootnamespace$
    

    【讨论】:

    • 这很好用,而且不像名字所暗示的那样,它将文件添加到的文件夹的完整命名空间。
    【解决方案4】:

    我找到了一种解决方法 - 通过使用自定义模板向导,并在源代码管理资源管理器中检索当前选定的项目。

    由于 CUSTOM 项目模板仅在解决方案树中选择项目节点时在“添加新”菜单中可用,因此可以确定项目名称(以及 EnvDTE 可用的所有其他信息)并添加到替换字典中。

    using EnvDTE80;
    
    public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams) 
    {
        //Adding by template is unavailable in the context menu when selecting more than one item -> use index 0. 
        UIHierarchyItem selectedUiHierarchyItem = (UIHierarchyItem )dte.ToolWindows.SolutionExplorer.SelectedItems[0]; 
    
        //not null when selecting solution node (when adding a project)
        Solution selectedSolution = selectedUiHierarchyItem.Object as Solution 
    
        //not null when selecting project node (when adding an item) or when selecting a solution folder (when adding a project)
        Project selectedProject = selectedUiHierarchyItem.Object as Project;
    
        //Determine which kind of project node: 
        if (selectedProject != null)
        {
            if (selectedProject.Kind == EnvDTE.Constants.vsProjectKindSolutionItems)
            {
                //solution folder
            }
            else
            {
                //project
                replacementsDictionary.Add("$projectname$", selectedProject.Name);
            }
        }
    }
    

    【讨论】:

    • 这为我指明了正确的方向。请注意,使用的“dte”变量是转换为 DTE2 的自动化对象,因此您需要引用 EnvDTE80
    • 感谢 Pedro - 我在顶部添加了一个 using 以使这一点更加清晰。
    • 你是怎么得到 dte 的?
    • 这个sn-p代码需要进入一个自定义项模板类——我只是从Visual Studio的模板中添加了一个,然后在顶部添加了这个。但那是 5 年前的事了,不知道今天的样子;)
    猜你喜欢
    • 2021-12-30
    • 2011-07-15
    • 2018-07-07
    • 1970-01-01
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多