When 元素 (MSBuild)

指定 Choose 項目可能要選取的程式碼區塊。

<專案><選擇><當><選擇> ...<否則><選擇> ...

語法

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

屬性和元素

下列章節說明屬性、子元素和父元素。

屬性

屬性 描述
條件 必要屬性。

要評估的條件。 如需詳細資訊,請參閱條件

子元素

元素 描述
選擇 選擇性項目。

評估子元素,以選取要執行的一個程式碼區段。 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>

另請參閱