When 元素 (MSBuild)

指定一个可能的代码块供 Choose 元素选择。

<Project><Choose><When><Choose> ... <Otherwise><Choose> ...

语法

<When Condition="'StringA'=='StringB'">
    <PropertyGroup>... </PropertyGroup>
    <ItemGroup>... </ItemGroup>
    <Choose>... </Choose>
</When>

特性和元素

下列各节描述了特性、子元素和父元素。

特性

属性 说明
条件 必需的特性。

要评估的条件。 有关详细信息,请参阅条件

子元素

元素 说明
Choose 可选元素。

评估子元素以选择代码的一部分来执行。 Choose 元素中可能有零个或零个以上的 When 元素。
ItemGroup 可选元素。

包含一组用户定义的 Item 元素。 ItemGroup 元素中可能有零个或零个以上的 When 元素。
PropertyGroup 可选元素。

包含一组用户定义的 Property 元素。 PropertyGroup 元素中可能有零个或零个以上的 When 元素。

父元素

元素 说明
Choose 元素 (MSBuild) 评估子元素以选择代码的一部分来执行。

备注

如果 Condition 属性的计算结果为 true,则 When 元素的子级 ItemGroupPropertyGroup 元素将执行,且跳过所有后续 When 元素。

ChooseWhenOtherwise 元素一起用来提供一种方式,通过这种方式选择代码的一部分来执行许多种可能的替代选择。 有关详细信息,请参阅条件构造

示例

以下项目使用 Choose 元素来选择要在 When 元素中设置的属性值组。 如果两个 When 元素的 Condition 属性的计算结果均为 false,则将设置 Otherwise 元素中的属性值。 运行示例时,请尝试从命令行传入各种属性设置(例如 msbuild myproj.proj -p:Configuration=Test;Platform=x86),并查看输出路径的外观。 此示例假设需要为调试和发布版本设置某些属性(包括基于平台的位数而不是实际平台名称的输出文件夹),并支持“Test”和“Retail”配置,但将“Retail”视为“Release”。

<Project>
    <PropertyGroup>
       <Configuration Condition="$(Configuration) == ''">Debug</Configuration>
       <Platform Condition="$(Platform) == ''">x64</Platform>
    </PropertyGroup>

  <Choose>
     <When Condition="$(Configuration)=='Test'">
        <PropertyGroup>
            <DebugSymbols>true</DebugSymbols>
            <DebugType>full</DebugType>
            <Optimize>false</Optimize>
            <DefineConstants>DEBUG;TRACE</DefineConstants>
        </PropertyGroup>
        <Choose>
          <When Condition="$(Platform)=='x86' Or $(Platform) == 'ARM32'">
            <PropertyGroup>
                <OutputPath>.\bin\Test\32-bit\</OutputPath>
            </PropertyGroup>
          </When>
          <When Condition="$(Platform)=='x64' Or $(Platform) == 'ARM64'">
            <PropertyGroup>
                <OutputPath>.\bin\Test\64-bit\</OutputPath>
            </PropertyGroup>
          </When>
          <!-- For any other platform, use the platform name -->
          <Otherwise>
            <PropertyGroup>
              <OutputPath>.\bin\Test\$(Platform)\</OutputPath>
            </PropertyGroup>
          </Otherwise>
        </Choose>
      </When>
      <When Condition="$(Configuration)=='Retail' Or $(Configuration)=='Release'">
        <PropertyGroup>
            <DebugSymbols>false</DebugSymbols>
            <Optimize>true</Optimize>
            <DefineConstants>TRACE</DefineConstants>
        </PropertyGroup>
        <Choose>
          <When Condition="$(Platform)=='x86' Or $(Platform) == 'ARM32'">
             <PropertyGroup>
                <OutputPath>.\bin\Release\32-bit\</OutputPath>
             </PropertyGroup>
          </When>
          <When Condition="$(Platform)=='x64' Or $(Platform) == 'ARM64'">
             <PropertyGroup>
                <OutputPath>.\bin\Release\64-bit\</OutputPath>
             </PropertyGroup>
          </When>
          <!-- For any other platform, use the platform name -->
          <Otherwise>
            <PropertyGroup>
                <OutputPath>.\bin\Release\$(Platform)\</OutputPath>
            </PropertyGroup>
          </Otherwise>
        </Choose>
      </When>
      <!-- For any other configuration, use debug properties-->
      <Otherwise>
        <PropertyGroup>
            <DebugSymbols>true</DebugSymbols>
            <DebugType>full</DebugType>
            <Optimize>false</Optimize>
            <DefineConstants>DEBUG;TRACE</DefineConstants>
        </PropertyGroup>
        <Choose>
          <When Condition="$(Platform)=='x86' Or $(Platform)=='ARM32'">
            <PropertyGroup>
              <OutputPath>.\bin\$(Configuration)\32-bit\</OutputPath>
            </PropertyGroup>
          </When>
          <When Condition="$(Platform)=='x64' Or $(Platform)=='ARM64'">
            <PropertyGroup>
              <OutputPath>.\bin\$(Configuration)\64-bit\</OutputPath>
            </PropertyGroup>
          </When>
        </Choose>
       </Otherwise>
  </Choose>

  <Target Name="ShowProperties">
    <Message Text="DebugSymbols: $(DebugSymbols)"/>
    <Message Text="Optimize: $(Optimize)"/>
    <Message Text="DefineConstants: $(DefineConstants)"/>
    <Message Text="OutputPath: $(OutputPath)"/>
  </Target>
</Project>

另请参阅