IPlatformInitializer in Prism For Xamarin Forms Version 7

Introduction

The implementation of platform specific services has undergone some changes over the last few releases. At first, if a view model required a service, it would check the Prism container for implementations of a service. If it wasn’t registered there, it would look in the Xamarin Forms dependency injection system for the implementation. In the next iteration, it was recommended that developers use the IPlatformInitializer that was typed to the specific container that was being used in the project.

Using IPlatformInitializer was a better solution: if you use the Xamarin Forms dependency service it involved a call to a static instance that is hard to mock for your unit tests. So that wasn’t great for us. But there was still some improvments the Prism team wanted to make. The next (and current) release of the library is going to abstract the container type away from the library users so that the container type can be swapped with minimal fuss. So that means changes to the IPlatformInitializer interface.

So how does this work now?

Define Your Interface in the Shared Library

As an example, we could have a bluetooth service that communicates with some device. This is defined in either your shared project or some other project that is shared between all of your platform specific projects.

public interface IBtDeviceCommService
{
    Task<bool> InitAsync();
    Task<bool> SendCommandAsync(string command);
}

Implement Platform Specific Services

Next we need to define the classes in each of the platform projects. I am not going to show the actual implementation of the platform specific classes, but it would look something like the following

public class UwpBtDeviceCommService : IBtDeviceCommService
{
    public async Task<bool> InitAsync()
    {
        throw new NotImplementedException();
    }

    public async Task<bool> SendCommandAsync(string command)
    {
        throw new NotImplementedException();
    }
}

Setup the IPlatformInitializer For Each Platform

How do we get the platform specific implementation registered within the Prism architecture? Looking at the UWP platform again, we can perform the registrations with the following piece of code.

public class UwpPlatformInitializer : Prism.Ioc.IPlatformInitializer
{
    public void RegisterTypes(IContainerRegistry containerRegistry)
    {
        containerRegistry.Register(typeof(IBtDeviceCommService), typeof(UwpBtDeviceCommService));
    }
}

The way the platform initializer gets called is that you pass it in as a parameter when you create the Prism Application object. In the UWP project, there is a default MainPage object. In the constructor for the MainPage, the application object is created and loaded. It looks like the following piece of code.

public sealed partial class MainPage
{
    public MainPage()
    {
        this.InitializeComponent();
        LoadApplication(new MyPrismApplicationClass(new UwpPlatformInitializer()));
    }
}

In the Android platform project, you create the Prism Application object in the MainActivity class. In the iOS project, the Prism Application object is created in the AppDelegate class.