Sharing Code Part II: Partial Classes

May 10, 2013 — 4 Comments

In the first part of Sharing Code series 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 partial classes to help share code across our many apps on Windows 8, Windows Phone 7 & Windows Phone 8 using the same source code from Part I.

What is a Partial Classes?

Essentially a partial class allows you to separate a class or structure into two or more separate files. Personally, I use this technique to hide any specific platform code from other platforms.

Implementation of Partial Class

We’ll use this in a the Math2 class to create a Divide() method that returns a float.

NOTE: you can create the float Divide() method within the same class but for demonstration purposes we’ll assume we want to have the float Divide() hidden from WP8_App and W8_App

In WP7_App, open up Math.cs and add the partial keyword to the class. You should end up with something like

Next, add a new class called Math_WP7.cs and change the implementation to the following

Updating the UI

To use the new overloaded Divide() method, we’ll have to add some code to our UI, Update the btnDivide.Click() handler to the following

Now, when you run the code, you will get a float value returned and shown in the display.

9

Another Example

Although the example above shows how to use partial classes, the example is pretty basic. A real world example could be getting the device Unique Id.

In the WP7_App project, add a class called DeviceInfo and change the implementation to the following

Add another class file called DeviceInfo_WP7.cs and change the implementation to the following

Now open MainPage.xaml.cs and in the constructor add the following line

Run the app and in the console output window, you will see the device unique ID displayed.

Why do this?

If you have done Windows Phone 8 or Windows 8 development and want to get the device unique ID, you will know that the code to get that information is different. Partial classes will help us solve this while maintaining a common API across the different platforms which is Common.DeviceInfo.Default.DeviceId.

In WP8_App and W8_App add the following line to the MainPage.xaml.cs constructor

Add the DeviceInfo.cs file to the WP8_App and W8_App projects making sure to add it as a link to the file. In the W8_App project, create a new file called DeviceInfo_W8.cs with the following implementation

Now in the WP8_App project, create a new file called DeviceInfo_WP8.cs with the following implementation

NOTE: you will have to add ID_CAP_IDENTITY_DEVICE to your WMAppManifest.xml for this to work

Now when you run the code on all platforms

  1. You have a different ID returned on all three platforms
  2. You use a common API DeviceInfo.DeviceId property to acess the device ID

You can download the source code off github.

Up Next

In 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 Code.

 

 

 

Trackbacks and Pingbacks:

  1. Sharing Code Part I: Common Source Files | Mark Arteaga - May 13, 2013

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

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

    […] Sharing Code Part II: Partial Classes (source code download) […]

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

    […] Sharing Code Part II: Partial Classes (source code download) […]

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

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

Leave a Reply