Sharing Code Part I: Common Source Files

May 9, 2013 — 4 Comments

In this article, I’ll go through how to share source code files between Windows Phone 8, Windows Phone 7.1 and Windows 8.  In this sample we’ll go through how to share a Math class between two separate Windows Phone projects.

Creating the Projects

First thing is to create two Visual Studio 2012 Projects one for Windows Phone 7.1, 8 and Windows 8.

Windows Phone 7.1 Project

Create a Windows Phone Project and call it WP7_App and uncheck ‘Create Directory for solution’.


In the next dialog select Windows Phone OS 7.1


At this point your project should be generated.

Windows Phone 8 Project

Now we need to create a Windows Phone 8 project and essentially, these are the same steps as the 7.1 project.

Open a new Visual Studio 2012 window and create a new Windows Phone Project. Name the project WP8_App and uncheck Create directory for solution.


In the next dialog, select Windows Phone 8.0 as the target platform.


Click ok and the Windows Phone 8 project will be created.

Windows 8

Next we need to create our Windows 8 project. Open a new Visual Studio 2012 window and create a Windows 8 C# project. Name the project W8_App and uncheck Create directory for solution.


Now you should have three projects in the same directory as follows


Next we’ll actually start sharing some code.

The Math Class

In this example we’ll be creating a sample math class that will be used and expanded on in future parts. We’ll be using a singleton pattern for the class for ease of use throughout the different platforms.

In the WP7_App project, add a class called Math.cs. Change the namespace to Common and add the following code.

As you can see it’s a very simple class that performs some simple math operations. Next we’ll need to add some code to call this class from the UI.

Updating the UI

In the WP7_App, WP8_App & W8_App projects. Open up MainPage.xaml and update the ContentPanel xaml in the Windows Phone projects with the following

In the Windows 8 project, you will be replacing the main grid on the page.

On Windows Phone, the changes will result with the following.


On Windows, this will result in the following


The UI is not the greatest, but will leave it to reader to fix.

Code Behind

Open up MainPage.xaml.cs in all three projects and add the following helper properties

Now add the following to the constructor

Run the WP7_App project, add some numbers for X & Y and click one of the buttons. If you try to run on Windows 8 or Windows Phone 8, you will get compile errors which we will fix in the next section.

NOTE: I will leave it up to you to add error handling.

Sharing the Code

Now that all the projects and code are setup we are ready to use our first technique for code sharing which is sharing the Math.cs file across all platforms.

Note: Technically I did use a code sharing technique across all platforms called copy paste of code. This is not a technique you want to use because maintenance of the code will be a nightmare.

In the WP8_App project click on Project – Add Existing Item and navigate to the WP7_app project to find the Math.cs file. Select the file, then on the Add button click the dropdown and select Add As Link. Do the same in the W8_App project.

Now when you run the project on Windows Phone 7 or 8 and Windows 8, the projects will compile and run.

The advantage of this, one code change you make to the Math.cs all projects will get the changes. You will require a re-compile, but you will have less code to maintain. You can download the source code from github.

Up Next

In the next part, we’ll look at using Partial Classes to try and share some more code and we’ll look at some of the UI Code.


Trackbacks and Pingbacks:

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

    […] the first part of Sharing Code we looked at sharing a common class source file across three different platforms. Continuing the series on Sharing Code across various platforms, we’ll look at leveraging […]

  2. Sharing Code Part III: Conditional Compile | Mark Arteaga - May 13, 2013

    […] 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 […]

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

    […] Sharing Code Part I: Common Source File (source code download) […]

  4. Sharing Code Across Platforms: Introduction | Mark Arteaga - May 13, 2013

    […] Sharing Code Part I: Common Source File (source code download) […]

Leave a Reply