【问题标题】:Adding references in Visual Studio project template?在 Visual Studio 项目模板中添加引用?
【发布时间】:2017-10-10 18:08:38
【问题描述】:

我正在为 VS2015 创建一个多项目模板,其中一个创建的项目引用另一个。如何使用模板添加引用?

如果我使用 VS GUI 添加引用,它会将以下内容添加到 .vcxproj 文件中:

<ItemGroup>
<ProjectReference Include="path\xyz.vcxproj">
<Project>{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}</Project>
</ProjectReference>

并且 GUID 是有效的,因为 VS 知道所引用项目的 GUID。当我从模板创建新项目时,我不知道将为新创建的项目选择什么 GUID,因此我无法使用模板参数添加有效的 GUID。我可以轻松正确地添加Include="..." 部分,但我没有GUID。

我也找不到关于 &lt;ProjectReference&gt; 标记的太多信息,但似乎需要有效的 GUID,将 &lt;Project&gt; 标记留在外面会导致引用不起作用,如果我使用全零也会发生同样的情况GUID。

【问题讨论】:

    标签: visual-c++ visual-studio-2015 project-template


    【解决方案1】:

    您可以在解决方案模板中生成 GUID 并将它们传递给项目。因此,您需要解决方案模板中 ProjectTemplateLink 中的参数 CopyParameters。 示例:

    <ProjectTemplateLink ProjectName="$safeprojectname$.Data"  CopyParameters="true">
    

    您的项目模板必须替换参数

    <Project TargetFileName="Data.vbproj" File="Data.vbproj" ReplaceParameters="true">
    

    现在您可以从项目文件中的解决方案模板访问参数并设置外部 guid 参数(注意对外部参数的访问具有“$ext_”)

     <ProjectGuid>{$ext_guid1$}</ProjectGuid>
    

    对于具有引用的项目,您使用 ItemGroup 中的 ProjectReference

    <ItemGroup>
      <ProjectReference Include="..\$ext_safeprojectname$.Data\$ext_safeprojectname$.Data.vbproj">
        <Project>{$ext_guid1$}</Project>
        <Name>$ext_safeprojectname$.Data</Name>
      </ProjectReference>
    </ItemGroup>
    

    使用 Visual Studio 2017 和 .NET Framework 4.5.2 对我有用

    【讨论】:

      【解决方案2】:

      就我而言,我在主项目中有 2 个项目引用。它在 Visual Studio 2019,Net Core 3 中为我工作。作为@skaddy 的答案,但在几次 Visual Studio 重新启动并最终机器重新启动(这很糟糕)之后却没有。

      不同的是,我不关心 guid,我只是让 Visual Studio 关心。

      步骤

      1. 在解决方案模板中,我设置的 Root.vstemplate CopyParameters="true"
          <ProjectTemplateLink ProjectName="$safeprojectname$.API" CopyParameters="true">
              APITemplate\MyTemplate.vstemplate
          </ProjectTemplateLink>
      
      1. 我确保 ReplaceParameters="true" 存在于主项目模板中(从一开始就存在)。
          <TemplateContent>
              <Project TargetFileName="APITemplate.csproj" File="APITemplate.csproj" ReplaceParameters="true">
                  ...
              <\Project>
              ...
          <\TemplateContent>
      
      1. 在主 .csproj 文件中,我查看引用并使用参数更改它们

      从这里

          <ItemGroup>
              <ProjectReference Include="..\Data\Data.csproj"/>
              <ProjectReference Include="..\Service\Service.csproj"/>
          </ItemGroup>
      

      到这里

          <ItemGroup>
              <ProjectReference Include="..\$ext_safeprojectname$.Data\$ext_safeprojectname$.Data.csproj"/>
              <ProjectReference Include="..\$ext_safeprojectname$.Service\$ext_safeprojectname$.Service.csproj"/>
          </ItemGroup>
      

      再一次,对我来说,这些参数在机器重启后才起作用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-17
        • 2012-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-29
        相关资源
        最近更新 更多