Xamarins.Essentials 和 Xamarin.Forms DependencyService 有什么区别

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

我是 Xamarin 的新开发人员,我在猜测 Xamarins.Essentials 和 Xamarin.Forms DependencyService 之间的区别是什么?

我了解到 Xamarins.Essentials 是一个提供跨平台 API 的框架,以便使用地理定位、连接等... DependencyService 是 Xamarin.forms 中的功能,以便使用 Android 或 iOS 平台功能。

我的理解对吗? 还有其他区别吗?

感谢您的帮助, 克莱门特

Essentials 是帮助程序 类 的集合,它允许执行核心 Xamarin Forms 库中未包含的操作。其中许多与利用平台特定行为有关。

DependencyService lightweight is a Dependency Injection 工具。在表单中,它通常用于访问特定于平台的行为。

如果 Essentials 可以做到,请使用 Essentials。如果没有,你必须自己写,你可能会使用 DependencyService.

你完全正确。

DependencyService 用于 "unveil" 共享项目的本机功能。基本上,您在 OS-specific 项目中编写 "native" 代码,这些代码可以通过依赖注入在您的共享项目中使用。 您可以在官方文档中阅读有关 DependencyService 的更多信息 - 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.

基本上,当涉及到选择时——如果它内置于 Essentials 中——请使用 Essentials。如果不是,自己写代码,通过DependencyService.

公开

是的。 Xamarin.Essentials 用于封装应用程序需要与平台硬件进行的许多交互,不幸的是(在撰写本文时)它不包含任何蓝牙功能。

Xamarin DependencyService 是一个支持基本依赖注入的框架实现,因此您的通用应用程序代码可以更轻松地与特定于平台的实现进行交互。

我认为大多数 App 开发人员可能会使用更通用的第三方包来处理依赖注入,例如Prism、Unity Container、AutoFac 等