Search code examples
c#.netwpfxamlicons

Different icons based on condition in csproj file


In my application I have two release configurations and I am able to change the splash screen based on the configuration with the condition attribute in SplashScreen like this

<ItemGroup>
    <None Remove="Resources\Splash screen\Logo1.png" Condition="'$(Configuration)'!='Release1'" />
    <None Remove="Resources\Splash screen\Logo2.png" Condition="'$(Configuration)'=='Release2'" />
</ItemGroup>

<ItemGroup>
    <SplashScreen Include="Resources\Splash screen\Logo1.png" Condition="'$(Configuration)'!='Release1'" />
    <SplashScreen Include="Resources\Splash screen\Logo2.png" Condition="'$(Configuration)'=='Release2'" />
</ItemGroup>

I want to achieve the same thing with the applicationIcon but there is no condition attribute, how do I do?

    <PropertyGroup>
        <OutputType>WinExe</OutputType>
        <TargetFramework>net8.0-windows</TargetFramework>
        <Nullable>enable</Nullable>
        <UseWPF>true</UseWPF>
        <Configurations>Debug;Release1;Release2</Configurations>
        <ApplicationManifest>app.manifest</ApplicationManifest>
        <RootNamespace>$(MSBuildProjectName.Replace(" ", ""))</RootNamespace>
        <ApplicationIcon>Resources\Icons\Icon1.ico</ApplicationIcon>
        <Platforms>AnyCPU;x64</Platforms>
    </PropertyGroup>

I also tried to do the same thing with the Icon in all the separate windows instead but without luck due to no condition attribute.


Solution

  • You can have conditional property groups, so you could do something like this:

      <PropertyGroup>
        <OutputType>WinExe</OutputType>
        <TargetFramework>net8.0-windows</TargetFramework>
        <Nullable>enable</Nullable>
        <UseWPF>true</UseWPF>
        <Configurations>Debug;Release1;Release2</Configurations>
        <ApplicationManifest>app.manifest</ApplicationManifest>
        <RootNamespace>$(MSBuildProjectName.Replace(" ", ""))</RootNamespace>
        <Platforms>AnyCPU;x64</Platforms>
      </PropertyGroup>
    
      <PropertyGroup Condition="'$(Configuration)'=='Release1'">
        <ApplicationIcon>Resources\Icons\Icon1.ico</ApplicationIcon>
      </PropertyGroup>
      <PropertyGroup Condition="'$(Configuration)'=='Release2'">
        <ApplicationIcon>Resources\Icons\Icon2.ico</ApplicationIcon>
      </PropertyGroup>