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:
- 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.
- Similar features with different API implantations some of these features are:
- Application lifecycle (PLM)
- Tiles and toast notifications
- System services
- Networking differences
- Background processing
- Image/video capture
- 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.
- 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.
- 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:
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)
string output = await tcs.Task;
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.