Corrado Cavalli
Published in

Corrado Cavalli

MSBuild Conditions and Visual Studio

Don’t trust Visual Studio…

My goal was to include a different file inside my project depending on selected project configuration, something that MSBuild is able to do using an appropriate Condition applied at item level or, in case of multiple items involved, at ItemGroup level.
After adding the two files to my project and customizing their content, I’ve edited the .csproj file and added following statements:

<ItemGroup>
<AppxManifest Include="Package.debug.appxmanifest" Condition="'$(Configuration)'=='Debug'">
<SubType>Designer</SubType>
</AppxManifest>
<AppxManifest Include="Package.release.appxmanifest" Condition="'$(Configuration)'!='Debug'">
<SubType>Designer</SubType>
</AppxManifest>
</ItemGroup>

but the result was that inside Visual Studio both files were listed and since the app can’t have more than one .appxmanifest file the project couldn’t build.

Two files aren’t better than one

After several unsuccessful tentative I then tried with another cool way to define non mandatory content inside a .csproj via the Choose element.

<Choose>
<When Condition="'$(Configuration)'=='Debug'">
<ItemGroup>
<AppxManifest Include="package.debug.appxmanifest" >
<SubType>Designer</SubType>
</AppxManifest>
</ItemGroup>
</When>

<Otherwise>
<ItemGroup>
<AppxManifest Include="Package.release.appxmanifest" >
<SubType>Designer</SubType>
</AppxManifest>
</ItemGroup>
</Otherwise>
</Choose>

but also this alternative apparently was not working and i started getting lost until my friend Damiano hinted me at this old StackOverflow post where it is mentioned that Visual Studio (2010! at that time) doesn’t filter item out based on applied condition. 🙈

So in the end everything was working, just a little ”annoyance” that after several editions of Visual Studio is still there.
I hope in future Visual Studio will dim out project items not satisfying a condition so that it will be immediately evident that that file is not part of the build.

--

--

--

I’m a Senior Software Engineer at Microsoft, former Xamarin/Microsoft MVP and, when not in front of a monitor, a passionate mountain biker and ski tourer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Corrado Cavalli

Corrado Cavalli

Senior Sofware Engineer at Microsoft, former Xamarin/Microsoft MVP mad about technology. MTB & Ski mountaineering addicted.

More from Medium

Xamarin Forms Multilingual App

WP CLI: Most Useful Commands

WP CLI Usefull Commands

How to send Email automatically from Google Sheet via C#

mail automation cover image

Visual Studio 2022 Browser Edge with Your Profile