Dependency Injection with PCLs in Xamarin

A Portable Class Library (PCL) allows you to target the platforms you wish to support, and use Interfaces to provide platform-specific functionality. This is also known as dependency injection. You will often run into limitations with PCLs with UI-related needs which require specialized handling on each platform. In my example I will be using the Xamarin.Mac and .NET Framework 4.5 Frameworks. Other frameworks such as Windows 8, Xamarin.Android and Xamarin.iOS will automatically be included. This is a desktop app that runs on Windows and OSX, and shares code with a PCL.

Creating the Solution and Projects

I began by creating a new project in Visual Studio. Search for Portable. I chose Class Library (Portable) under C#. I did not choose the Class Library (Xamarin.Forms Portable) because I am not referencing that library in my non-mobile application. Name it Portable, select your frameworks and create.

Next, I renamed my solution to Proj. I just wanted to distinguish it from my different projects. All your files are stored in a root folder named Portable. This can be renamed later to match the solution name if desired. I then right-clicked on my solution to add a new project. I chose WPF Application and named it Win. I set it as my startup project. On my OSX Laptop I also opened the same solution in Xamarin Studio. It showed the Win project as unsupported and that’s fine. On the solution I went to Add New Project. Under Mac App I chose a General Empty Project. I named it Mac. Once complete set it as the Startup Project. Back in Windows I reopened my solution it gave me an Unsupported notice and migration report about the Mac project. Disregard.

PCL Code

Now let’s write some code. In my example I am going to retrieve Machine Information. In my experience I have struggled to find a PCL nuget that could fetch detailed platform machine information. Under my Portable I added a new class called IPlatform. Inside it I created an interface:
Next, I created an PlatformManager class with the following code:
Next, I added a third class to the portable. I named it StaticUtils

Windows Project Code

Now we need to create the proper hooks in our Win project. In my app.xaml.cs file I added the following:
Next, I added a class called WinInterface I added this code:

Mac Project Code

In my AppDelegate.cs file I added the following:
Next, I added a class called MacInterface I added this code:

How It All Works

The platform project starts and instantiates their class of methods implementing the portable interface. We then call an assignment method in the portable’s StaticUtils class. The assignment method instantiates a new, static PlatformManager for us to reference, and the manager stores the passed interface into a variable within itself. With a static reference we can access it through the life on the application. Now, when we want to fetch our platform method details we reference the static PlatformManager, which references the interface that implements the platform-specific code. Calling it like so will return the desired data:
Download a working example developed in Visual Studio 2013.