Sharing Code Part IV: Portable Class Libraries

May 11, 2013 — 2 Comments

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

  1. Sharing Code Part I: Common Source File (source code download)
  2. Sharing Code Part II: Partial Classes (source code download)
  3. 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

  1. .NET Framework
  2. Silverlight
  3. Windows Phone 7
  4. Windows Phone 8
  5. Windows 8 (Store Apps)
  6. XBox 360

and is great for developing an API/SDK to share externally with developers.

Math2 Class

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)

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.

  1. Switch the configuration to Release
  2. Build the Project (Build- Build Solution)

In the WP7_App project,

  1. Delete the Math.cs and Math_WP7.cs file
  2. Click on Project – Add Reference
  3. Click on Browse
  4. Navigate to the location of the Math2 project and location the bin/Release directory
  5. 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.

Trackbacks and Pingbacks:

  1. Sharing Code between Windows Phone and Windows 8, Part IV: Portable Class Libraries - May 13, 2013

    […] /* 0) { ratingctl00_cphMiddle_cphContent_tr3w45fwwvre_itemRating = result['Rating']; SetCurrentRating('ctl00_cphMiddle_cphContent_tr3w45fwwvre_itemRating_pnlHolder', result['Rating'], "disabled fullStar", "disabled emptyStar", true); if(votesCountctl00_cphMiddle_cphContent_tr3w45fwwvre_itemRating!=null) { votesCountctl00_cphMiddle_cphContent_tr3w45fwwvre_itemRating ++; SetVotesCount('ctl00_cphMiddle_cphContent_tr3w45fwwvre_itemRating_lblUsersRated', '(' + votesCountctl00_cphMiddle_cphContent_tr3w45fwwvre_itemRating + ' votes)'); } SetRatingCookie('r', 'i33799', '1'); } else if (result['Status'] == 1) { alert('The session has expired. Please refresh the page to be able to vote!'); } } /* ]]> */ (0 votes) 0 comments   /   posted by Silverlight Show on May 13, 2013 Tags:   windows-phone , windows-8 , mark-arteaga Read original post at Mark Arteaga's Blog […]

  2. Windows 8 Spotlight #7 - Ibrahim KIVANC - DPE Technical Evangelist - Site Home - MSDN Blogs - June 21, 2013

    […] Sharing Code Part: Portable Class Libraries […]

Leave a Reply