In the last three posts, I have gone through various ways to share common code across the various Windows based platforms. So far in this series of articles in sharing code we have looked at
- Sharing Code Part I: Common Source File (source code download)
- Sharing Code Part II: Partial Classes (source code download)
- Sharing Code Part III: Conditional Compile (source code download)
In this final part, we’ll look at using a Portable Class Library to share the Math2 class across the different platforms instead of the C# source code file. Personally, I prefer sharing the source code file across the platforms and not sharing an assembly as it gives a little more control. Of course, if I’m building out an API or SDK for external developers to use, a Portable Class Library is the way to go.
What is a Portable Class Library?
Essentially a portable class library is DLL that can be shared with developers and has a common set of code that will work on various platforms without modifications. It can contain business logic or in our example, methods to do some math calculations. You can use a PCL on
- .NET Framework
- Windows Phone 7
- Windows Phone 8
- Windows 8 (Store Apps)
- XBox 360
and is great for developing an API/SDK to share externally with developers.
As our Math2 class is essentially our business logic, it’s a good candidate for moving into a Portable Class Library.
Open a Visual Studio Window and create a new Portable Class Library project called Math2
Click OK and in the next dialog select the frameworks you would want to support. In our case we are going to support Windows Phone 7 and higher and .NET for Windows Store apps.
Click OK and the new project will be created.
Rename Class1.cs to Math2.cs and add the following implementation (same as what we currently have)
public partial class Math2
private static Math2 _default;
public static Math2 Default
if (_default == null)
_default = new Math2();
public int Add(int x, int y)
return x + y;
public int Subtract(int x, int y)
return x - y;
public int Multiple(int x, int y)
return x * y;
public int Divide(int x, int y)
return x / y;
public float Divide(float x, float y)
return x / y;
Reference the PCL
There are two ways to reference the Portable Class Library either by including the project in your app solution or by compiling to PCL to release build and referencing the DLL produced.
In our case, we are going to compile in release and reference the assembly in all our projects.
- Switch the configuration to Release
- Build the Project (Build- Build Solution)
In the WP7_App project,
- Delete the Math.cs and Math_WP7.cs file
- Click on Project – Add Reference
- Click on Browse
- Navigate to the location of the Math2 project and location the bin/Release directory
- Select the Math2.dll and click Add
Run the project and everything should work as previously.
Do the same to the WP8_App and W8_App projects, run them and similar to the WP7_App, the apps will run as expected.
Hopefully you got something out of these articles, my son learned a lot especially why i spend so much time in front of my computer. The final code you can download from GitHub.