在 Visual Studio 项目模板中添加引用? [英] Adding references in Visual Studio project template?

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

问题描述

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

I'm creating a multi-project template for VS2015 where one of the created projects references the other. How do I add the reference using the template?

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

If I add the reference using the VS GUI it will add the following to the .vcxproj file:

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

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

And the GUID is a valid one, since VS knows the GUID of the referenced project. When I create the new projects from template I don't know what GUID will be chosen for the newly created project, so I can't add a valid GUID by using the template parameters. I can easily add the Include="..." part correctly, but the GUID I don't have.

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

I couldn't find much info on the <ProjectReference> tag either, but it seems that a valid GUID is required, leaving the <Project> tag out causes the reference to not work, and same happens if I use all zeros GUID.

推荐答案

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

You can generate the GUIDs in the solution template and pass them to the projects. Therefor you need the parameter CopyParameters in the ProjectTemplateLink in Solution Template. Example:

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

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

Your project template has to replace parameters

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

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

Now you can access the parameters from solution template inside the project file and set the external guid parameter (note the acces to the external parameter have a "$ext_")

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

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

For the project with the reference you use the ProjectReference inside ItemGroup

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

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

It worked for me using Visual Studio 2017 with .NET Framework 4.5.2

这篇关于在 Visual Studio 项目模板中添加引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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