Continuation of Windows First- A New Strategy for Mobile Development and Beyond
With a working application in place, we then demonstrated how to refactor the project code in preparation for supporting additional platforms. We decided to create a Portable Class Library to share code across platforms.
Adding a Portable Class Library to the solution
As shown below, we are targeting not only .NET-based applications, but we are also planning to support iOS and Android as well. We will get to that soon enough.
Portable Class Library target platforms
Note that the more platforms you target within your PCL, the narrower the scope becomes for supported APIs across those platforms, which results in a reduced ability for code reuse. However, in our case, we have the potential to reuse both the Model and ViewModel. We refactored the application to move the Session model and SessionViewModel from the Shared App into the Portable Class Library (PCL) project. Within the Windows 8.1 and Windows Phone 8.1 projects, we simply added a project reference to the PCL.
Refactored solution to leverage PCL for code reuse
Last but not least, we demonstrated how to expand your application’s reach to iOS and Android markets by creating a cross platform version of your Windows-based app using Xamarin, and leveraging PCLs to enable code reuse within your iOS and Android applications.
Xamarin comes installed with a standalone IDE, Xamarin Studio, as well as a plug-in to Visual Studio, allowing you to continue to develop in the environment that you are already comfortable working within. Whether you are working in Xamarin Studio or Visual Studio, you can expand your application to the Android and iOS platforms by continuing to develop in C#.
It is important to note that Xamarin products are commercially licensed products, and you must pay for a license when you are ready to develop and publish apps to iOS and/or Android markets.
We also discussed the importance of learning about the user experience on each platform, as well as the importance of gaining an understanding of Android and iOS development at a high level, which will help you to understand how to structure your app and design your UI for each platform, because additional coding will be required within these platform-specific applications.
Our final demo consisted of a sample Android application which leveraged our PCL to pull in data within the app. We designed the Android UI to render a different layout than its Windows and Windows Phone counterparts.
Adding a Xamarin.Android application to the solution
Xamarin.Android project also references the PCL
In this simple example, we leverage the SessionViewModel from the Portable Class Library to load the data, and then display it within the UI. As I mentioned earlier, we decided to change the UI layout for the Android application. We chose to display the session data up front, without a list, and added navigation buttons to drive the navigation from session to session. Additional methods were coded into the SessionViewModel to manage the page navigation, without affecting the Windows 8.1 and Windows Phone 8.1 existing behavior.
Basic implementation to demonstrate loading data from the PCL within the Android application
As shown below, the look and feel of our Android app is completely different, but the data being used is still the same among all platforms.
When making the decision to create mobile applications, Universal Apps allow you to use the skills you already have to target Windows Store and Windows Phone platforms simultaneously, giving you the advantage of publishing a cross platform product quickly and easily. When you are ready to expand to other platforms, refactor your code to leverage Portable Class Libraries, to enable code reuse among your Xamarin.iOS and Xamarin.Android apps when you are ready.
In case you missed it last week, here’s the video of the session from ObjectSharp’s At the Movies event.