The DependencyService is a dependency solver that allows applications to call on platform-specific functionalities from shared code.
This feature therefore allows Xamarin.Forms applications to do everything a native application can do.
In practice, an interface is defined and the DependencyService In order to ensure the correct implementation of this interface from the different platform projects.
How do we use it?
- Declare an interface common to all platforms.
- Implement the interface on each platform
- Record the implementation of the interface
- Recover the instance implementing the interface
Declare the interface
The first step is to declare an interface for the functionality you want to use on your OS. For our example we'll share the d’URL sharing feature on iOS and Android.
Déclearance of l’common interface between OS:
public class IShareService
{
void Share( string url);
}
Implementing the interface
ImpléThis is not a problem.
public class DroidShareService : IShareService
{
public void Share(string url)
{
var intent = new Intent(Intent.ActionView);
intent.SetData(Android.Net.Uri.Parse(url));
intent.SetFlags(ActivityFlags.ClearTop);
intent.SetFlags(ActivityFlags.NewTask);
Application.Context.StartActivity(intent);
}
}
Impléuse of iOS :
public class iOSShareService : IShareService
{
public void Share(string url)
{
UIApplication.SharedApplication.OpenUrl(new NSUrl(url));
}
}
Registration
Then for each platform you'll have to register the services.
The first way is by using the attribute Dependency Attribute as follows:
[assembly: Xamarin.Forms.Dependency (typeof (DroidShareService))]
The second way is to call the method Register of DependencyService:
Xamarin.Forms.DependencyService.Register<DroidShareService>();
Note: When you target UWP it is best to use the second method because when you compile in .NET Native it may delete your type. Dependency Attribute.
Recover the instance implementing the interface
To recover the instance implementing the interface nothing simpler, simply call the static method Get out<TInstance> of the type DependencyService
var shareService = Xamarin.Forms.DependencyService.Get<IShareService>();
shareService.Share("https://www.https://blogs.infinitesquare.com/");
How does this work?
At the first call of the method Get out<T> the DependencyService will initialize its list of implementations by searching all assemblies for the uses of the attribute Dependency Attribute.
Initialization:
Type targetAttrType = typeof(DependencyAttribute);
// Don't use LINQ for performance reasons
// Naive implementation can easily take over a second to run
foreach (Assembly assembly in assemblies)
{
Attribute[] attributes = assembly.GetCustomAttributes(targetAttrType).ToArray();
if (attributes.Length == 0)
continue;
foreach (DependencyAttribute attribute in attributes)
{
if (!DependencyTypes.Contains(attribute.Implementor))
{
DependencyTypes.Add(attribute.Implementor);
}
}
}
Note: if your application contains many assemblies, this first initialization may take some time.
Then once the DependencyTypes lists are initialized it dynamically creates the service instance through the class The activator.
(T)Activator.CreateInstance(dependencyImplementation.ImplementorType);