Xamarin.Forms is a cross-platform toolkit that allows developers to easily create native user interface layouts that can be shared across Android, iOS, and Windows platform. Xamarin.forms provides us with DependencyService – a dependency resolver, in which an interface is defined and DependencyService finds the correct implementation of that interface from the various platform projects..

DependencyService allows apps to call into platform-specific functionality from shared code. This functionality enables Xamarin.Forms apps to do anything that a native app can do.With Dependency service we can access lot of native functionalities for ex: Media (Camera), Network connectivity, Battery Services, Location Services etc.
In this blog we will see how to make use of dependency service for Network connectivity. Although network connectivity plugins are available just to make it simple let’s make use of this native feature.
Create interface in PCL:
We will create an interface IConnectivity in the PCL project with a method definition as IsConnected – Which will return true if the device is connected to internet or false.
public interface IConnectivity { bool IsConnected(); }
Platform Specific Implementation:
We need to implement IConnectivty in each platform that we are targeting in the app. For example, the below NetworkConnectivityImpl class implements the IConnectivty interface in Android platform.
Note : Permission to access internet needs to be enabled in the specific platform.
class NetworkConnectivityImpl : IConnectivity
{
public bool IsConnected()
{
var connectivityManager =
(ConnectivityManager)Xamarin.Forms.Forms.Context.GetSystemService(Context.ConnectivityService);
var networkAction = connectivityManager.ActiveNetworkInfo;
return networkAction.IsConnected;
}
}
Note : Every implementation must have a default constructor for Dependency Service to be able to instantiate it.
Registering the service:
Each implementing class must be registered with Dependency Service via a metadata attribute. Registration enables Dependency Service to find the implementing class and supply it in place of the interface at run time. The following code registers the implementation for android platform:
using XamarinForms_DependencyService.Droid.DependencyServiceImpl; [assembly:Xamarin.Forms.Dependency(typeof(NetworkConnectivityImpl))] namespace XamarinForms_DependencyService.Droid.DependencyServiceImpl { class NetworkConnectivityImpl : IConnectivity { .... } }
Putting it all together the platform specific implementation looks like this:
[assembly:Xamarin.Forms.Dependency(typeof(NetworkConnectivityImpl))] namespace XamarinForms_DependencyService.Droid.DependencyServiceImpl { class NetworkConnectivityImpl : IConnectivity { public bool IsConnected() { var connectivityManager = (ConnectivityManager) Xamarin.Forms.Forms.Context.GetSystemService(Context.ConnectivityService); var networkAction = connectivityManager.ActiveNetworkInfo; return networkAction.IsConnected; } } }
Note: that the registration is performed at the namespace level, not the class level.
Call to Dependency Service:
Once the interface and implementations for each platform are done, use the DependencyService to get the right implementation at runtime:
private void OnCheckNetwork(object sender, EventArgs e) { IsConnected = DependencyService.Get().IsConnected() ? "Active Network Connection" : "No Active Internet"; }
Note : You must provide an implementation in every platform project. If no Interface implementation is registered, then the DependencyService
will be unable to resolve the Get()
method at runtime in the unimplemented platform.


Summary
In this blog we have learnt what is Dependency service and how to use dependency service to access Native feature on different platforms in Xamarin.Forms app. Source code for this sample is available for download in this Repo.