Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
996 views
in Technique[技术] by (71.8m points)

visual studio - Why the `Condition` attribute doesn't work for the `ItemGroup` element?

MS Visual Studio 2013.

Both When and ItemGroup elements can to have the Condition attribute (as I see in MSDN). But I get different results.

When $(CAD_Year) is 2015, I expect the AcRibbon will be not referensed to my project:

<ItemGroup Condition= "'$(CAD_Year)' &lt; '2010'" >    
  <Reference Include="AcRibbon">
    <HintPath>$(CAD_SDK_Location)$(Inc)AcRibbon.dll</HintPath>
    <Private>False</Private>
  </Reference>
</ItemGroup>

But I get unresolved reference in the Solution Browser for the AcRibbon always.

But this variant works fine:

<Choose>
  <When Condition= "'$(CAD_Year)' &lt; '2010'">
    <ItemGroup>
      <Reference Include="AcRibbon">
        <HintPath>$(CAD_SDK_Location)$(Inc)AcRibbon.dll</HintPath>
        <Private>False</Private>
      </Reference>
    </ItemGroup>
  </When>
</Choose>

At this case the AcRibbon is referenced only when $(CAD_Year) less than 2010. Why I get the different results?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I found the answer in MSDN here:

While conditional import statements work in command-line MSBuilds, they do not work with MSBuild in the Visual Studio integrated development environment (IDE). Conditional imports are evaluated by using the configuration and platform values that are set when the project is loaded. If changes are subsequently made that require a reevaluation of the conditionals in the project file, for example, changing the platform, Visual Studio reevaluates the conditions on properties and items, but not on imports. Because the import conditional is not reevaluated, the import is skipped. To work around this, put conditional imports in the .targets files or put code in a conditional block such as a Choose Element (MSBuild) block.

I think this is true for ItemGroup element also...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...