Click here to Skip to main content
15,907,905 members
Articles / Desktop Programming / XAML

Conditional Compilation in Universal Apps

Rate me:
Please Sign up or sign in to vote.
4.94/5 (15 votes)
2 Jul 2015CPOL7 min read 52.1K   365   23   18
Sharing code and views in Universal Windows Apps


Conditional compilation is the process of defining compiler directives that cause different parts of the code to be compiled, and others to be ignored. This technique can be used in a cross-platform development scenario to specify parts of the code that are compiled specific to a particular platform.[1] Image 1

Conditional compilation allows the compiler to skip some parts of the source code when compiling based on the conditional clauses. The #if ,#endif  #elif and #else directives are used for conditional compilation. These directives add conditions to parts of a source file. With the use of these directives we can keep a single source file.

//some code

//some code


It tells the compiler which code is to compile for the project.

The value of the condition determines which code block is included.

    Any identifier-or-keyword except true or false

conditional compilation directives should always be written as sets consisting of, in order, an #if directive, zero or more #elif directives, zero or one #else directive, and an #endif directive. Between these directive we have to write the conditional code.

Suppose if we have some code to be only compile if a certain condition is true we will define a condition with an ordinary if else statement. This is very helpful if we are working on a shared project.

Here is another C# example-

public int calculate(int input)
   int result;
   result= input + 10;
   result= input - 10;


   return result;  

The value of Condition will determine the functionality


         result =  input + 10;

CONDITION is not set 

         result =  input - 10;


When to use Conditional Compilation

Using Conditional Compilation we can isolate platform-specific code so, we can use this anywhere according to our need. But if we are working on a large project it will make our code unmanageable. .Conditional compilation cannot be used in Portable Class Library because when it is compiled it generates only a single binary file .


Conditional Compilation in Windows Apps

In universal apps by using conditional compilation we can write code that can target different platforms i.e. Windows store and Windows Phone. We can choose which code is to compile for different project as both the project are compiled to different packages

This is the simplest way to define platform-specific code . For example, we could attempt to share our code base for  Windows Phone and Windows Store apps, and change how your code behaves for each platform by surrounding platform-specific code with a condition that can be set depending on which app or platform we are compiling for. This will be more clear with the following example

Here is an example of conditional compilation in Universal windows Apps

First we are creating a C# universal app using a blank template. The solution is created using three projects a Windows Store app, a Windows Phone app and a Shared Project.

Image 2

The first thing we notice in the solution is that both the windows store and windows phone projects have a separate start page which is the Main page. But we have to share this page so we will just drag one of them to the shared project and then we will delete the Main page from the other two projects. It wont create any problem as it is just a blank page.

Now we have a common shared Main page.


Image 3



Image 4

In the Main page of shared project we are creating a simple UI, just a Button with an event handler and a TextBlock  in which we want to display   “Windows Store” if it is a Windows store app or “Windows Phone” if it is a Windows Phone app on a button click.

We will do this with conditional compilation in button click event handler

We will add conditions



We can see this condition by going to  Properties->Build->Condition compilation symbols

Here in Windows properties the Conditional compilation symbol defined are NETFX_CORE and WINDOWS_APP and in Windows phone properties, NETFX_CORE and WINDOWS_PHONE_APP .

Windows 8  have a compiler directive set called NETFX_CORE. So, this is always set for a Windows 8 project and common in both Windows store and Windows phone.

Even we can add our compilation condition here, say "MY_COMPILATION_CONDITION"  and can use this in our project but let’s go with the default one.


Image 5


Our condition here is, if it is a windows phone application we want to display “Windows Phone” else we want to display “Windows store”. So in the event handler we will use Condition Compilation

Image 6


We can see some code here is greyed out, this is just because intellisense is helping us with our conditional compilation. We can swap from windows to windows phone from the dropdown menu at the top. We can see here our platform is set to Windows , so the code related to other platform is greyed out. This is a very helpful feature as we can easily notice which code is going to compile specific to platforms.


Image 7


Right click on the Windows project and select "Set as startup project" Now if we run the windows app by clicking on the button we get

Image 8

 And if we select Windows phone as startup project and run the project , Windows phone project will be running and we can see it is displaying Windows Phone instead of Windows Store.

Image 9


An alternative way to share the view is

Conditional Compilation in XAML

If we use a TopAppBar in our shared project the application will run perfectly in windows store but we will get an exception in windows phone as top app bar is not available for windows phone.

There is a good way to manage differences between of XAML between Windows and Windows Phone is by using XAML Conditional Compilation. XCC is a preprocessor adding conditional compilation support to XAML files. We can enable XCC in your project by installing a tiny NuGet package.It supports Windows Universal apps and Xamarin Forms projects.

We will add this through nuget package manager. We will do this on solution level as we have to add this to both the projects. Whenever we reference any library or package from the shared project it needs to be added to both the projects.

Open the solution context menu and select "Manage NuGet Package for Solution". Search for xcc and install the XAML Conditional Compilation NuGet package in both projects.

Image 10


After we have added the nuget package we have to declare it in the Main page where we want to use it.

We are adding the conditions for windows app and windows phone app




The xmlns:win and xmlns:wp attributes define the namespaces for conditional compiled XML elements and attributes.  xmlns:win is used when we want the code to be compile for only windows store app and xmlns:wp is used when we want the code to be compiled for only Windows Phone app.

We have also to add it to be ignored else VS will give errors

mc:Ignorable="d win wp"

The mc:Ignorable attribute ensure the new xmlns prefixes are ignored by designers and compilers.

With the conditional compilation namespaces in place we can now enter the following XAML.Now we have to just add a prefix “win” or “wp” to our controls. Here in our example we want our TopAppBar to show only when it is a Windows Store application so we will add “win” to “Page.TopAppBar”.

I have also added “wp” to “Page.BottomAppBar”. Now it will only compile to windows Phone application.


Image 11


If we run Windows App it will show only the Top App Bar

Image 12


If we run Windows Phone App it will show only the Command Bar.

Image 13


Intellisense does not work when we apply a custom prefix to an XML element. But we  can enable Intellisense support by adding the  mc:ProcessContent attribute.

mc:ProcessContent="win:* wp:*"

 The mc:ProcessContent attribute enables Intellisense inside conditional regions.


Tips and Best practices

Don’t use  #else if, #elseif or #elsif they won't work. You can use #elif instead.

XCC is in beta version so use it carefully.

If we use XCC to particular control, we won’t be able to see it at design time. My trick is to design it first and when I am satisfied with it I apply the Conditional Compilation to it.


Points of Interest

Universal Windows apps are time saving. We can build an app for Windows and Windows Phone at the same time, and share code, user controls, styles, strings, and other assets between the two projects in Visual Studio. This reduces the time and expense associated with building and maintaining an app for each type of device.


V1 of article.


References Image 14 Image 15 Image 16


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior)
India India
Pooja Baraskar is an accomplished software engineer, inventor, and technical author with over a decade of experience in the tech industry. With a passion for innovation and a commitment to excellence, Pooja has established herself as a thought leader and an agent of change in the industry. Currently, Pooja is leading various developer programs globally at Intel, leveraging her expertise and vision to drive impactful initiatives that shape the industry and empower tech professionals around the world.
As a tech trailblazer, Pooja is passionate about challenging the status quo and driving innovation. She is a pioneer in areas such as artificial intelligence and the Internet of Things and has a proven ability to translate complex technical concepts into accessible and actionable ideas. She has an impressive track record of delivering results in complex, high-stakes projects and is continually seeking new ways to drive value and make a difference.
Pooja is also an accomplished technical author. Her writing is known for its clarity, accessibility, and technical accuracy, making her a trusted source of information for developers and tech enthusiasts around the world. Digital courseware For Educators for OpenVINO developer by her has been used to teach AI in several universities across the globe, helping to equip the next generation of tech professionals with innovative skills. Pooja is an active member of the tech community, sharing her expertise through speaking engagements, workshops, and mentorship. Her contributions to the field have been widely recognized, including being named a Top Software Innovator by Intel and a Most Valuable Professional by Microsoft.
In addition to her work in the tech industry, Pooja is an avid traveler, passionate cook, and skilled martial artist. Her diverse interests and experiences have given her a well-rounded perspective on life and work, allowing her to approach challenges with creativity, energy, and enthusiasm.

Comments and Discussions

QuestionVoting of 5 Pin
Ganesan Senthilvel1-Jul-15 20:15
Ganesan Senthilvel1-Jul-15 20:15 
GeneralMy vote of 5 Pin
Renju Vinod23-Dec-14 0:23
professionalRenju Vinod23-Dec-14 0:23 
GeneralRe: My vote of 5 Pin
Pooja Baraskar23-Dec-14 1:37
professionalPooja Baraskar23-Dec-14 1:37 
GeneralMy vote of 5 Pin
Christian Amado18-Dec-14 2:04
professionalChristian Amado18-Dec-14 2:04 
GeneralRe: My vote of 5 Pin
Pooja Baraskar18-Dec-14 4:17
professionalPooja Baraskar18-Dec-14 4:17 
QuestionArent "Universal App" the new kid in town Pin
KarstenK12-Dec-14 4:37
mveKarstenK12-Dec-14 4:37 
AnswerRe: Arent "Universal App" the new kid in town Pin
Pooja Baraskar12-Dec-14 6:08
professionalPooja Baraskar12-Dec-14 6:08 
QuestionGood to see your article is live Pin
Grasshopper.iics25-Nov-14 21:44
Grasshopper.iics25-Nov-14 21:44 
AnswerRe: Good to see your article is live Pin
Pooja Baraskar26-Nov-14 1:03
professionalPooja Baraskar26-Nov-14 1:03 
GeneralRe: Good to see your article is live Pin
Grasshopper.iics26-Nov-14 1:08
Grasshopper.iics26-Nov-14 1:08 
GeneralRe: Good to see your article is live Pin
Pooja Baraskar26-Nov-14 16:50
professionalPooja Baraskar26-Nov-14 16:50 
GeneralMy vote of 5 Pin
Abhishek Nandy25-Nov-14 20:27
professionalAbhishek Nandy25-Nov-14 20:27 
GeneralRe: My vote of 5 Pin
Pooja Baraskar26-Nov-14 1:01
professionalPooja Baraskar26-Nov-14 1:01 
GeneralRe: My vote of 5 Pin
Abhishek Nandy26-Nov-14 1:21
professionalAbhishek Nandy26-Nov-14 1:21 
You are welcome always Smile | :) Big Grin | :-D
GeneralMy vote of 5 Pin
jixuyang25-Nov-14 15:15
jixuyang25-Nov-14 15:15 
GeneralRe: My vote of 5 Pin
Pooja Baraskar25-Nov-14 18:20
professionalPooja Baraskar25-Nov-14 18:20 
GeneralMy vote of 5 Pin
Robert J. Good25-Nov-14 9:46
professionalRobert J. Good25-Nov-14 9:46 
GeneralRe: My vote of 5 Pin
Pooja Baraskar25-Nov-14 18:21
professionalPooja Baraskar25-Nov-14 18:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.