简体   繁体   中英

What is the difference between Xamarins.Essentials and Xamarin.Forms DependencyService

I'm a new developer on Xamarin and I was guessing what is the difference between Xamarins.Essentials and Xamarin.Forms DependencyService?

I understood that Xamarins.Essentials is a framework providing cross-platform API in order to use geolocation, connectivity etc... Whereas DependencyService is functionality in Xamarin.forms in order to use the Android or iOS platforms functionalities.

Is my understanding right? Are there other differences?

Thank you for your help, Clement

Essentials is a collection of helper classes that allow to do things that aren't baked in to the core Xamarin Forms library. Many of them relate to utilizing platform specific behavior.

DependencyService lightweight is a Dependency Injection tool. In Forms it is often used to access platform specific behavior.

If Essentials can do it, use Essentials. If it doesn't and you have to write it yourself, you will possibly use DependencyService .

You are totally correct.

DependencyService is being used in order to "unveil" native functionality to your shared project. Basically, you write "native" code in the OS-specific projects, which through Dependency injection can be used in your shared project. You can read more for DependencyService in the official docs - Introduction & Registration & Resolution

Xamarin.Essentials is your must-have toolkit. It supports anything from Geolocation, through Battery & Preferences. The good thing with Essentials is that it strips any code that it is not using. So, even though it has many functionalities, during building & archiving, it only leaves what is referenced through your code. Essentials is also open-source - repo .

Basically, when it comes to a choice - if it is built-in into Essentials - use Essentials. If it is not, write your own code and expose it through DependencyService .

Yes. Xamarin.Essentials is there to encapsulate many of the interactions that Apps require with the platform hardware, unfortunately (at the time of writing) it doesn't include any Bluetooth functionality.

Xamarin DependencyService is a framework implementation to support basic dependency injection so that your common App code can interact with platform specific implementations more easily.

I think that most App developers probably a more versatile, third party package to handle dependency injection eg Prism, Unity Container, AutoFac etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM