Sharing Code Part III: Conditional Compile

May 11, 2013 — 2 Comments

In the first part of Sharing Code series we looked at sharing a common class source file across three different platforms, and in the second part we continued with using partial classes to separate the code out. In this third part of the series, we’ll look at using conditional compiles in our code and continue working on the sample code from the previous articles.

What is Conditional Compile?

Conditional compile is essentially telling the compiler to either compile a piece of code or to ignore it. For example the following will compile the code if it is a DEBUG build but this code will not compile in RELEASE build.

DEBUG and RELEASE are pre-defined in your project, but you can also define your own directives as follows

Whenever you define your directive, you will have to define it at the beginning of your CS file or you can define it in your project properties in the Build tab as follows


Now that we have a overview on conditional compiles, we can look at how to implement in the sample code.

Sharing the UI Code

If you have been following along in the series, you may have noticed that we have some common code in the MainPage.xaml.cs. Using a combination of conditional compiles and partial classes, we will change it so we have a common code base.

In the WP7_App project, add a class called MainPage_Common.cs with the following implementation

Delete the constructor, the X & Y properties from MainPage.xaml.cs. Run the project and essentially it will do the same thing as previously.

Updating Windows Phone 8 Project

In the WP8_App project, add the MainPage_Common.cs file to the project making sure to add the file as a link. Delete the constructor, the X & Y properties from MainPage.xaml.cs. When you try and compile the project, you will get around 16 various errors. Our main culprit is the following

Which is the namespace used in our Windows Phone 7 project. To fix this, we can use conditional compile statements. Open up the WP8_App project properties and in the Build tab set the Configuration to All Configurations and add WP8 to the Conditional Compile Symbols. Your project properties should look like the following


Now open MainPage_Common.cs and add the following for the namespace

Trying to compile the code, you will get an error which is a result of no Math2.Divide() method taking floats as a parameter. This method is only available in the WP7_App project and is by design. To fix this we can again add a conditional compile.

Back in WP7_App project, open the project properties and add a conditional compile called WP7. In MainPage_Common.cs change the btnDivide.Click implementation to the following

Compile and run the code on both and you will not get anymore compile time errors.

Updating the Windows 8 Project

Updating the Windows 8 project is essentially the same as the Windows Phone 8 project. You will have to

  1. Add the MainPage_Common.cs as a link to the project
  2. Remove the constructor in MainPage.xaml.cs
  3. Remove the X & Y properties in MainPage.xaml.cs
  4. Add a W8 as a Conditional Compile symbol in the project build properties
  5. Add the following to MainPage_Common.cs to allow the code to compile

Now when you run the projects you will get the same results. But the advantage now is you have a common code base across all three platforms to drive your app logic.

Up Next

In the next part, we’ll look at using Portable Class Libraries or managed DLLs across various platforms.



Trackbacks and Pingbacks:

  1. Sharing Code Part II: Partial Classes | Mark Arteaga - May 13, 2013

    […] the next part, we’ll look at using Conditional Compile statements to try and share some more code and we’ll look at some of the UI […]

  2. Sharing Code Part IV: Portable Class Libraries | Mark Arteaga - May 13, 2013

    […] Sharing Code Part III: Conditional Compile (source code download) […]

Leave a Reply