Microsoft announced the acquisition of Xamarin, a leading platform provider for mobile app development, at the BUILD developer conference in March 2016. Available at no extra cost, developers can download Xamarin for Visual Studio or Xamarin Studio and start writing apps for iOS, Android, and Windows using C#. Moving forward, Xamarin will be included with every version of Visual Studio, as well as Visual Studio Community Edition.

This announcement is big news for developers who write apps across platforms. Using Visual Studio (C# or F#), developers can deliver a fully native mobile app experience on all major devices. Breakout sessions and labs focusing on Xamarin filled up quickly with developers eager to learn more details. In one session, Xamarin co-founder Miguel de Icaza, shared some technical insights of how Xamarin integrates with .NET, and how developers can take advantage of Xamarin's integration with Visual Studio.

Shared Logic

Figure 1: Xamarin's Cross-platform approach

Using Native Shared project types, we can share code between all three platforms. For example, a banking app will likely require the same security methods for logging in, whether using an iPhone or a Windows phone. Using a shared project, we can write that security logic and share it with all three OS-specific projects. A web project could also be added and share the same logic. Applications can share logic using two different project types:

  • Shared Asset Projects - Simplest method of sharing code between unique platform projects
  • Portable Class Libraries (PCL) - .NET functionality limited by the platforms being targeted, but allows code sharing between Xamarin.Android, Xamarin.iOS, and UWP

Let's examine a health management application made available during a lab at BUILD. It's available on Microsoft's GitHub site.

Note: When loading the project, you will be prompted to locate a Mac device since the Xamarin update does not include an emulator for iOS. Remote login needs to be enabled on a Mac device to debug iOS apps.

Figure 2: Application Stub

Almost all application logic is in a PCL, MyHealth.Client.Core (Portable), representing the shared portion of the application. The other three projects, MyHealth.Client.Droid, MyHealth.Client.iOS, and MyHealth.Client.W10.UWP (Universal Windows) are the platform-specific projects referencing platform-specific APIs.

This simple example demonstrates how a data binding class can be shared between platform-specific project types.

In HomeView.cs, beginning on line 24, a simple getter property, HomeViewModel, returns an iOS-specific binding context (ViewModel) that is cast to a HomeViewModel type.

On line 109 of the same file, we can see that FirstMedicineCountdown, defined in the core project, is referenced.

For Android, the HomeViewModel properties are exposed by way of a generic class of type HomeViewModel.

Similar to iOS, the Windows project exposes the HomeViewModel using a simple getter inside the HomeView.xaml.cs code-behind.

Platform-Specific Projects

What about platform-specific API compliance? According to Microsoft, developers have full API access within each platform. Since an OS-specific project has a unique UI layer, UI APIs are unique to project types. The Android.Widget namespace, for instance, is only available in the Xamarin.Android project type.

Managing platform-specific projects within a solution is not unlike managing different project types in a traditional Visual Studio solution. Different projects for data access, business logic, and presentation layers will reference specific assemblies for that project type. Given Xamarin.iOS and Xamarin.Android are just project types, they too can only reference assemblies that are available for those project types. The References and Components (only for iOS and Android) sections inside each project contain the OS-specific assemblies.

Here are the different project types currently supported on Xamarin for Visual Studio:

  • Android
  • Apple Watch
  • Cross-Platform
  • iPad
  • iPhone
  • Universal (UWP)

Incorporating Xamarin with Visual Studio creates many opportunities for cross-platform developers. Using a single language, applications can share common logic, while providing full native API compliance to all major platforms.

About the Author

Jeremy McBrideJeremy is a software development consultant in the Washington, DC Metro office with expertise in the Microsoft technology stack. He has experience architecting and implementing enterprise applications facilitating ASP.NET, WebAPI, and Windows Mobile. His most recent projects include leading a Windows 10 UWP development team and providing Android development support.