将CustomParameter与Visual Studio多项目模板一起使用 [英] Using CustomParameter with Visual Studio Multi-Project Template

查看:143
本文介绍了将CustomParameter与Visual Studio多项目模板一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够创建一个包含以下两个项目的解决方案的多项目模板(其中的界面应包含对业务层的引用).

  • <proj_name>.interface
  • <proj_name>.business.

通常,您可以包含 $safeprojectname$ ,其中包含:

用户在新建项目"对话框中提供的名称

但是,正如文章 VS 2010多项目模板:项目间参考,通过使用 CustomParameters ,但遇到了麻烦./p>

我的压缩模板目录如下:

  • MultiTemplate.vstemplate
  • 接口
    • InterfaceTemplate.vstemplate
    • MyTemplate.Interface.csproj
  • 业务
    • BusinessTemplate.vstemplate
    • MyTemplate.Business.csproj

您可以下载整个目录,但这是一些精选的片段

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">
        Interface\InterfaceTemplate.vstemplate
      </ProjectTemplateLink>
      <ProjectTemplateLink ProjectName="$safeprojectname$.Business">
        Business\BusinessTemplate.vstemplate
      </ProjectTemplateLink>
    </ProjectCollection>
  </TemplateContent>
</VSTemplate>
 

Interface \ InterfaceTemplate.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$部分不会被替换.相反,它保持不变.

:如何正确地将这些信息从多项目模板"传递给每个子模板?

如果可以弄清楚如何替换<name>标记值,则奖励点,因为代币替换似乎不起作用.

解决方案

Visual Studio 2013 Update 2最终通过 <ProjectTemplateLink ProjectName="$safeprojectname$.Interface" CopyParameters="true"> Interface\InterfaceTemplate.vstemplate </ProjectTemplateLink>

然后您可以像这样在孩子.csproj中实现您的目标:

 <ItemGroup>
  <ProjectReference Include="..\$ext_safeprojectname$.Business\$ext_safeprojectname$.Business.csproj">
    <Project>{E5511F75-5B9C-4816-B991-E09225661CF4}</Project>
    <Name>MyTemplate.Business</Name>
  </ProjectReference>
</ItemGroup>
 

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).:

  • <proj_name>.interface
  • <proj_name>.business.

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

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.

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.

My Zipped Up Template Directory looks like this:

  • MultiTemplate.vstemplate
  • Interface
    • InterfaceTemplate.vstemplate
    • MyTemplate.Interface.csproj
  • Business
    • 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">
        Interface\InterfaceTemplate.vstemplate
      </ProjectTemplateLink>
      <ProjectTemplateLink ProjectName="$safeprojectname$.Business">
        Business\BusinessTemplate.vstemplate
      </ProjectTemplateLink>
    </ProjectCollection>
  </TemplateContent>
</VSTemplate>

Interface\InterfaceTemplate.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>

The Problem

When I create a new project, the $SolutionName$ portion of the string does not get replaced. Instead it just stays the same.

Q: How can I properly pass this information from the Multi-Project Template to each of the child templates?

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 finally added a built-in way to do this with ProjectTemplateLink

The CopyParameters attribute will enable child access to all the variables of the parent template, with the prefix of ext_.

You don't even need CustomParameters for this. Change your ProjectTemplateLink to this:

<ProjectTemplateLink ProjectName="$safeprojectname$.Interface" CopyParameters="true">
  Interface\InterfaceTemplate.vstemplate
</ProjectTemplateLink>

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>

这篇关于将CustomParameter与Visual Studio多项目模板一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆