Introduction to Dependency Services in Xamarin Forms

tudip-logo

Tudip

23 January 2020

While developing a cross platform application using Xamarin Forms where a single code base is wrapped in a native application and code sharing is done, sometimes it may also require utilization of native features like Text to speech, Checking device orientation, Accessing local Storage etc. into the application.

In order to utilize the native feature into the Xamarin application from Shared Code, DependencyService comes into the picture and provides a way to call the platform specific feature and utilize it from the Shared Code in order to make Xamarin application to do everything that native application can do.

To understand the concept of the DependencyService and how to use it, let’s go through an example of Managing local storage platform specific (Android & iOS) using the dependency service.

To consume the DependencyService into the Xamarin Application following components needs be taken in care:

  1. Interface creation into PCL/Shared Project.
  2. Implementation of interface at platform level.
  3. Registration of the implementing classes into the DependencyService.
  4. Calling DependencyService.

1. Interface creation into PCL/Shared Project:

Interface creation let you design how to communicate to platform specific features, here we only declare what to communicate with the platform feature and leaves the implementation for “How to communicate” at platform level.

For our example, create an interface “ILocalStorageManager” into the PCL/Shared Project and add below code to capture date and time into the local storage when application is launched.

2. Implementation of interface at platform level:

After declaring the the interface, each targeting platform must implement the interface to utilize the feature.

For our example, create a class “LocalStorageManager” into both Android and iOS project to implement the “ILocalStorageManager” interface and add below code respectively.

LocalStorageManager.cs (Android) 
LocalStorageManager.cs (iOS)

3. Registration of the implementing classes into the DependencyService:

The class implementing the interfaces on each platform must be registered into the DependencyService with the metadata tag to identify the service to be executed while running the application.

In our example, registering the “LocalStorageManager” class into the DependencyService. Add the below code just above the implementing the classes namespace tag.

After registration it should look implementing classes should look a like as below:

LocalStorageManager.cs (Android) 
LocalStorageManager.cs (iOS)

4. Calling DependencyService:

Once the project setup is completed, interface is implemented at each targeting platform and interface implementing classes are registered, we can utilise the platform specific feature from the Shared Code using DependencyService.Use below code for calling the DependencyService.

DependencyService.Get<T>(); 

In our example, use the below code to call the DependencyService from the PCL/Shared project to store the application start time into the local storage.

Request a quote