我正在为 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。
我找不到关于 <ProjectReference>
的太多信息标签,但似乎需要有效的 GUID,留下 <Project>
标记出导致引用不起作用,如果我使用全零 GUID,也会发生同样的情况。
最佳答案
就我而言,我的主项目中有 2 个项目引用。它在 Visual Studio 2019、Net Core 3 中对我有用,如 @skaddy 答案,但在多次 Visual Studio 重新启动和最终机器重新启动后不起作用(这很糟糕)。
不同之处在于我不关心guid我只是让 Visual Studio 关心它。
步骤
- 在解决方案模板中,我将 Root.vstemplate 设置为 CopyParameters="true"
<ProjectTemplateLink ProjectName="$safeprojectname$.API" CopyParameters="true">
APITemplate\MyTemplate.vstemplate
</ProjectTemplateLink>
- 我确保主项目模板中存在 ReplaceParameters="true"(从一开始就是如此)。
<TemplateContent>
<Project TargetFileName="APITemplate.csproj" File="APITemplate.csproj" ReplaceParameters="true">
...
<\Project>
...
<\TemplateContent>
- 在主 .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>
同样,对我来说,这些参数在机器重新启动后才起作用。
关于visual-c++ - 在 Visual Studio 项目模板中添加引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46673216/