Building a shared core between Windows 8 & Windows Phone 8

So a week ago I was a building an application that was planned to be running on both windows 8 and windows phone 8 and since both of them are running on the same logic and both of them was written in C# and XAML it should be very easy.

So after 2 days, I faced 2 problems with my approach:

  1. That both of the UI are using XAML doesn’t mean that they are the same both of the have their own components and navigation system you can see some examples in the images below.
  2. Similar features with different API implantations some of these features are:
    1. Application lifecycle (PLM)
    2. Tiles and toast notifications
    3. System services
    4. Networking differences
    5. Background processing
    6.  Image/video capture
    7. App bar

So in order to solve these problems I had to abstract the application logic from the UI and I achieved that by using MVVM, It’s very similar to MVP (model-view presenter) and MVC (model-view-controller).

MVVM is an architectural pattern that is composed of three parts:

  • Model handles the data.
  • View Model handles the logic of converting the model data into data that the view can use.
  • View handles the UI Using MVVM helps structure your code to make it reusable between Windows 8 and Windows Phone 8 Both platforms use XAML and support data binding.
So I found a very easy tutorial on how to implement MVVM you can find it in the video below presented by Jerry Nixon and you can see his full blog post here:
For the Second problem there is two ways to solve that:
  1. The First one is to add a Portable Class Library which is used to create a cross platform library I only have to choose which platforms I am targeting and it will only show common API between these platforms and will compile my code in the end into a dll that can be referenced from both platform this feature is only available in non express editions of visual studio.
  2. The second option is Conditional compilation which works by adding some keywords and depending on the platform I am targeting the code will be compiled for example:
using System.Net.Http;


namespace CommonLibrary
public class CommonServices
public async static Task<string> Request(string url, string param)
HttpClient client = new HttpClient();
return (await client.GetAsync(url)).ToString();
var tcs = new TaskCompletionSource<string>();
WebClient client = new WebClient();
client.DownloadStringCompleted += (s, e) =>
if (e.Error == null)
client.DownloadStringAsync(new Uri(url));
string output = await tcs.Task;
return output;
return "";

What the above code does is the following it checks if the platform running is windows 8 it will use httpclient while on the other hand if the platform is windows phone 8 it will run the webclient by using the above code I built a class that can be referenced in both windows phone 8 and windows 8 applications. By following these two approaches I was able to solve my problems and build better reusable code that targets both platforms. I hope this post was helpful to you if you have any questions I’ll be glad to answer them.

Comments (9)

  1. hany says:

    Great post. One comment/ask: can you in your next post highlight 2 api implementations for a feature and how you fixed the problem? Just pick any feature you like.

  2. shehab fawzy says:

    I'll be doing that thank you for the comment

  3. Stuart Lodge says:

    Excellent post – good to see more about code sharing.

    Please don't use conditional compilation – it breaks too often, especially if you refactor 🙂

    PCLs are good – and the PCL team have a portable HTTPClient library available on nuget especially for your use case.

    Interested to keep reading more 🙂

    Stuart (very, very biased when it comes to cross-platform PCL Mvvm coding –…/MvvmCross)

  4. koko says:

    really like … looking forward to move my apps to w8 …  I remember what u told me

  5. shehab fawzy says:

    Thank you Stuart I'll be taking you tips into consideration.

  6. shehab fawzy says:

    🙂 I'll be waiting for that Koko and if need any help you've got my email.

  7. Great 1 Shehab.. very helpful