Using Dependency Services in Xamarin
Introduction
Dependency services in Xamarin allow you to call platform-specific functionality from shared code. This enables you to access native APIs and features that are not directly available through Xamarin.Forms.
Setting Up Dependency Services
To use dependency services, you need to define an interface in your shared code and implement it in the platform-specific projects.
Defining the Interface
public interface IDeviceInfo
{
string GetModel();
string GetVersion();
}
Implementing the Interface
Next, implement the interface in each platform-specific project.
Android Implementation
[assembly: Dependency(typeof(DeviceInfoService))]
namespace MyXamarinApp.Droid
{
public class DeviceInfoService : IDeviceInfo
{
public string GetModel()
{
return Android.OS.Build.Model;
}
public string GetVersion()
{
return Android.OS.Build.VERSION.Release;
}
}
}
iOS Implementation
[assembly: Dependency(typeof(DeviceInfoService))]
namespace MyXamarinApp.iOS
{
public class DeviceInfoService : IDeviceInfo
{
public string GetModel()
{
return UIKit.UIDevice.CurrentDevice.Model;
}
public string GetVersion()
{
return UIKit.UIDevice.CurrentDevice.SystemVersion;
}
}
}
Using Dependency Services
Finally, use the dependency service in your shared code to call the platform-specific functionality.
Using the Service in Shared Code
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
var deviceInfo = DependencyService.Get<IDeviceInfo>();
modelLabel.Text = deviceInfo.GetModel();
versionLabel.Text = deviceInfo.GetVersion();
}
}
Complete Example
Here is the complete example with all the components put together.
MainPage.xaml
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyXamarinApp.MainPage">
<StackLayout>
<Label x:Name="modelLabel" Text="Model"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
<Label x:Name="versionLabel" Text="Version"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage>
MainPage.xaml.cs
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
var deviceInfo = DependencyService.Get<IDeviceInfo>();
modelLabel.Text = deviceInfo.GetModel();
versionLabel.Text = deviceInfo.GetVersion();
}
}
IDeviceInfo.cs
public interface IDeviceInfo
{
string GetModel();
string GetVersion();
}
DeviceInfoService.cs (Android)
[assembly: Dependency(typeof(DeviceInfoService))]
namespace MyXamarinApp.Droid
{
public class DeviceInfoService : IDeviceInfo
{
public string GetModel()
{
return Android.OS.Build.Model;
}
public string GetVersion()
{
return Android.OS.Build.VERSION.Release;
}
}
}
DeviceInfoService.cs (iOS)
[assembly: Dependency(typeof(DeviceInfoService))]
namespace MyXamarinApp.iOS
{
public class DeviceInfoService : IDeviceInfo
{
public string GetModel()
{
return UIKit.UIDevice.CurrentDevice.Model;
}
public string GetVersion()
{
return UIKit.UIDevice.CurrentDevice.SystemVersion;
}
}
}