CodySig
CodySig

Reputation: 194

Xamarin DependencyService vs Device.OnPlatform

Would someone be able to explain the difference between using a DependencyService versus the Device.OnPlatform feature in an Xamarin PCL project? I have a basic overview of what they both are from reading the documentation on Xamarin but I am still a little lost on what exactly the difference is. They seem like they provide the same functionality but maybe I just have a completely wrong understanding of these features.

Upvotes: 1

Views: 240

Answers (1)

Sharada
Sharada

Reputation: 13611

DependencyService allows you to pull and access the native implementation for a particular contract from a platform-specific library into your cross-platform library.

While, Device.OnPlatform allows you to define different actions for each native platform in the cross-platform library itself.

If you need to define platform-specific behavior/action(s) that interact with native OS framework(s) (such as Xamarin.Android, Xamarin.iOS etc.) - then you use DependencyService to allow access to this behavior in shared code. For example: you can use it to implement and use a device-status-helper in your portable library.

If you need to define platform-specific behavior/action(s) that don't need to interact with native frameworks but just shared ones (such as Xamarin.Forms, RESX etc.) - then you use Device.OnPlatform to define them in shared code. For example: You can use it to define platform-specific style(s) for a forms button. (can be defined in both C# and XAML).

Upvotes: 2

Related Questions