Visual c++ 是否在Visual Studio项目模板中添加引用?

Visual c++ 是否在Visual Studio项目模板中添加引用?,visual-c++,visual-studio-2015,project-template,Visual C++,Visual Studio 2015,Project Template,我正在为VS2015创建一个多项目模板,其中一个创建的项目引用另一个。如何使用模板添加引用 如果我使用VS GUI添加引用,它将向.vcxproj文件添加以下内容: <ItemGroup> <ProjectReference Include="path\xyz.vcxproj"> <Project>{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}</Project> </ProjectReference>

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

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

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

{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX}
而且GUID是有效的,因为VS知道引用项目的GUID。当我从模板创建新项目时,我不知道将为新创建的项目选择什么GUID,因此无法使用模板参数添加有效的GUID。我可以轻松地正确添加
Include=“…”
部分,但我没有GUID


我也找不到关于
标记的太多信息,但似乎需要有效的GUID,保留
标记会导致引用不起作用,如果我使用全零GUID,也会发生同样的情况。

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


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

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

现在,您可以从项目文件内的解决方案模板访问参数,并设置外部guid参数(请注意,对外部参数的访问有一个“$ext\”)

{$ext\u guid1$}
对于具有引用的项目,使用ItemGroup内部的ProjectReference

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

{$ext_guid1$}
$ext\u safeprojectname$.Data

使用Visual Studio 2017和.NET Framework 4.5.2对我来说很有效。就我而言,主项目中有两个项目参考。在VisualStudio2019,NetCore3中,它对我起了作用。正如@skaddy所回答的,但不是在几个VisualStudio重新启动并最终重新启动机器之后(这太糟糕了)

不同之处在于我并不关心guid我只是让visualstudio关心它

台阶

  • 在解决方案模板中,Root.vstemplate I set CopyParameters=“true”
  • 
    APITemplate\MyTemplate.vstemplate
    
  • 我确保ReplaceParameters=“true”存在于主项目模板中(从一开始就存在)
  • 
    ...
    ...
    
  • 在main.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>
    
    
    
    同样,对我来说,参数只是在机器重新启动后工作

        <ProjectTemplateLink ProjectName="$safeprojectname$.API" CopyParameters="true">
            APITemplate\MyTemplate.vstemplate
        </ProjectTemplateLink>
    
        <TemplateContent>
            <Project TargetFileName="APITemplate.csproj" File="APITemplate.csproj" ReplaceParameters="true">
                ...
            <\Project>
            ...
        <\TemplateContent>
    
        <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>