将 CustomParameter 与 Visual Studio 多项目模板一起使用
我希望能够创建一个 包含以下两个项目的解决方案的多项目模板(其中接口应包含对业务层的引用).
I would like to be able to create a Multi-Project Template for a solution which contains the following two projects (where the interface should include a reference to the business layer).:
.interface -
..business
通常,您可以包含 $safeprojectname$
,其中有:
Normally, you can include $safeprojectname$
, which has:
用户在新建项目对话框中提供的名称
The name provided by the user in the New Project dialog box
然而,正如文章中指出的那样构建多项目视觉工作室模板
However, as pointed out in the article Build a multi-project visual studio template
这里的问题是在每个子模板中,$safeprojectname$ 代表当前的项目名称.需要有一种方法可以从父模板中传入根 $safeprojectname$.
The problem here is that within each child template, $safeprojectname$ represents the current project name. There needs to be a way to pass in the root $safeprojectname$ from the parent template.
我正在尝试实施此 SO 问题中建议的解决方案 VS 2010 多项目模板:项目间参考,通过使用 CustomParameters
,但是我遇到麻烦了.
I'm trying to implement the solution suggested in this SO question VS 2010 Multi-Project Template: Inter-Project References, by using CustomParameters
, but am running into trouble.
我的压缩模板目录如下所示:
- MultiTemplate.vstemplate
- 界面
- InterfaceTemplate.vstemplate
- MyTemplate.Interface.csproj
- BusinessTemplate.vstemplate
- MyTemplate.Business.csproj
您可以下载整个目录,但这里有一些精选片段
You can download the Entire Directory, but here are some select snippets
MultiTemplate.vstemplate
<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="ProjectGroup"> <TemplateData><!--Removed for brevity --></TemplateData> <TemplateContent> <CustomParameters> <CustomParameter Name="$SolutionName$" Value="$safeprojectname$"/> </CustomParameters> <ProjectCollection> <ProjectTemplateLink ProjectName="$safeprojectname$.Interface"> InterfaceInterfaceTemplate.vstemplate </ProjectTemplateLink> <ProjectTemplateLink ProjectName="$safeprojectname$.Business"> BusinessBusinessTemplate.vstemplate </ProjectTemplateLink> </ProjectCollection> </TemplateContent> </VSTemplate>
InterfaceInterfaceTemplate.vstemplate
<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Project"> <TemplateData><!--Removed for brevity --></TemplateData> <TemplateContent> <Project TargetFileName="MyTemplate.Interface.csproj" File="MyTemplate.Interface.csproj" ReplaceParameters="true"> </Project> </TemplateContent> </VSTemplate>
MyTemplate.Interface.csproj
<ItemGroup> <ProjectReference Include="..$SolutionName$.Business$SolutionName$.Business.csproj"> <Project>{E5511F75-5B9C-4816-B991-E09225661CF4}</Project> <Name>MyTemplate.Business</Name> </ProjectReference> </ItemGroup>
问题
当我创建一个新项目时,字符串的
$SolutionName$
部分不会被替换.相反,它只是保持不变.The Problem
When I create a new project, the
$SolutionName$
portion of the string does not get replaced. Instead it just stays the same.问:如何将这些信息从多项目模板正确传递到每个子模板?
如果您能弄清楚如何替换
<name>
标记值,则奖励积分,因为令牌替换似乎不起作用.Bonus points if you can figure out how to replace the
<name>
tag value, as token replacements don't seem to work on it.
Visual Studio 2013 Update 2 终于通过 ProjectTemplateLink
Visual Studio 2013 Update 2 finally added a built-in way to do this with ProjectTemplateLink
CopyParameters
属性将允许子访问父模板的所有变量,前缀为 ext_
.
The CopyParameters
attribute will enable child access to all the variables of the parent template, with the prefix of ext_
.
为此您甚至不需要 CustomParameters.将您的 ProjectTemplateLink 更改为:
You don't even need CustomParameters for this. Change your ProjectTemplateLink to this:
<ProjectTemplateLink ProjectName="$safeprojectname$.Interface" CopyParameters="true">
InterfaceInterfaceTemplate.vstemplate
</ProjectTemplateLink>
然后你可以像这样在子.csproj
中实现你的目标:
And then you can achieve your goal in the child .csproj
like so:
<ItemGroup>
<ProjectReference Include="..$ext_safeprojectname$.Business$ext_safeprojectname$.Business.csproj">
<Project>{E5511F75-5B9C-4816-B991-E09225661CF4}</Project>
<Name>MyTemplate.Business</Name>
</ProjectReference>
</ItemGroup>