It is of no surprise that we would like to write code once and deploy everywhere. The time where an application is available exclusively to mobile platform is over. With that said, we can either develop for each of the three major platforms, or start investing in multiplatform deployment tools. In this article we will focus on using Portable Class Libraries (PCLs) to help speed up mobile development and consuming them in Xamarin projects.
Portable Class Library is a project type available in Visual Studio. The goal of using PCLs is to put as much of reusable logic into this as possible. What does that mean? You will want to put in your business logic as well as how you get data into this project. Here are the steps:
- Create New Project
- After creating the project go into the properties and change the target.
The key to using PCLs efficiently is determined here. You will want to go with the lowest common denominator. For example, if you choose Windows 8.1 and Windows Phone 8.1 you will not get data annotations because Windows Phone 8.1 will not support data annotations even though Windows 8.1 will. You can check out support features for .Net Framework, Windows Store, Windows Phone Store, Windows Phone Silverlight, Silverlight, and Xbox 360 here: https://msdn.microsoft.com/en-us/library/gg597391%28v=vs.110%29.aspx
Portable class libraries are available as a project type in Xamarin Studio, even if you are using the Starter Edition (Free). Using Xamarin Studio you can follow similar process:
Once you have your PCL coded, you can now add it to your Android or iOS Xamarin project. Now add the PCL as a reference in your mobile project.
Now you can use that reference in your code. You can use the same library for your Windows Phone apps, Windows Store apps, Xamarin Android app, and Xamarin iOS app. It sure beats having to rewrite the same code over and over again in multiple projects. This will leave one more time to focus on making richer UX designs.
You can download the demo project here: http://1drv.ms/1QmPWtd