Dependency Service in XF
Last updated
Last updated
While developing apps with Xamarin.Forms, you will find that certain native platform-specific functionalities are not present in the Xamarin.Forms API. This is because of the generic nature of Xamarin.Forms.
Xamarin.Forms allow 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.
You need to necessarily define an interface and write platform-specific implementations of that interface in the platform project. Dependency service will find the correct implementation of that interface in the various platform projects.
Xamarin.Forms apps need four components to use DependencyService:
Interface – The required functionality is defined by an interface in shared code. This needs to be implemented by each platform
Implementation Per Platform – The implementation of the above-mentioned interface for each platform.
Registration – To find the correct implementation of the interface for the suitable platform at runtime, it is required that each platform implementation class of the interface be registered with the DependencyService at runtime
Invoking Dependency Service- It is required that there be an explicit invocation of the Dependency service, which will then allow the Dependency service to choose the appropriate implementation based on the platform.