Adding references in Visual Studio project template?

爷,独闯天下 提交于 2021-02-19 05:55:07

问题


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?

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>

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.

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.


回答1:


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">

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>

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>

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




回答2:


In my case I have 2 project references in the main project. It worked for me in Visual Studio 2019, Net Core 3. as @skaddy answer, but not after several Visual Studio restarts and finally machine restart (that sucks).

The difference is that I did not care about the guid I just let Visual Studio care about that.

Steps

  1. In the solution template, the Root.vstemplate I set CopyParameters="true"
    <ProjectTemplateLink ProjectName="$safeprojectname$.API" CopyParameters="true">
        APITemplate\MyTemplate.vstemplate
    </ProjectTemplateLink>
  1. I ensure that ReplaceParameters="true" was there in the main project template ( it was since the beginning).
    <TemplateContent>
        <Project TargetFileName="APITemplate.csproj" File="APITemplate.csproj" ReplaceParameters="true">
            ...
        <\Project>
        ...
    <\TemplateContent>
  1. In the main .csproj file I look at the references and change them with the parameters

From this

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

To this

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

Again, for me the parameters just works after machine restart.



来源:https://stackoverflow.com/questions/46673216/adding-references-in-visual-studio-project-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!