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:
- 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.
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:
[View:https://www.youtube.com/watch?feature=player_embedded&v=cB7KdYPQw1k]
For the Second problem there is two ways to solve that:
- 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:
#if NETFX_CORE
using System.Net.Http;
#endif
#if WINDOWS_PHONE
#endif
namespace CommonLibrary
{
public class CommonServices
{
public async static Task<string> Request(string url, string param)
{
#if NETFX_CORE
HttpClient client = new HttpClient();
return (await client.GetAsync(url)).ToString();
#endif
#if WINDOWS_PHONE
var tcs = new TaskCompletionSource<string>();
WebClient client = new WebClient();
client.DownloadStringCompleted += (s, e) =>
{
if (e.Error == null)
{
tcs.SetResult(e.Result);
}
};
client.DownloadStringAsync(new Uri(url));
string output = await tcs.Task;
return output;
#endif
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
Anonymous
March 14, 2013
Really good postAnonymous
March 14, 2013
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.Anonymous
March 14, 2013
I'll be doing that thank you for the commentAnonymous
March 14, 2013
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 - github.com/.../MvvmCross)Anonymous
March 15, 2013
really like ... looking forward to move my apps to w8 ... I remember what u told meAnonymous
March 16, 2013
Thank you Stuart I'll be taking you tips into consideration.Anonymous
March 16, 2013
:) I'll be waiting for that Koko and if need any help you've got my email.Anonymous
March 18, 2013
Very nice approach....Anonymous
April 13, 2013
Great 1 Shehab.. very helpful