Xamarin.Forms DependencyService 简介

Download Sample下载示例

DependencyService 类是允许 Xamarin.Forms 应用程序从共享代码调用本机平台功能的服务定位器。

使用 DependencyService 调用本机平台功能的过程用于:

  1. 在共享代码中创建本机平台功能的接口。 有关详细信息,请参阅创建接口
  2. 在所需的平台项目中实现此接口。 有关详细信息,请参阅在各个平台上实现接口
  3. 通过 DependencyService 注册平台实现。 这使得 Xamarin.Forms 可以在运行时找到平台实现。 有关详细信息,请参阅注册平台实现
  4. 从共享代码解析平台实现,并调用它们。 有关详细信息,请参阅解析平台实现

下图说明了如何在 Xamarin.Forms 应用程序中调用本机平台功能:

Overview of service location using the Xamarin.Forms DependencyService class

创建接口

若要能够从共享代码调用本机平台功能,第一个步骤是创建接口来定义与本机平台功能交互的 API。 应将此接口置于共享代码项目中。

下面的示例说明了可用于检索设备方向的 API 接口:

public interface IDeviceOrientationService
{
    DeviceOrientation GetOrientation();
}

在各个平台上实现此接口

创建定义与本机平台功能交互的 API 的接口后,必须在各个平台项目中实现此接口。

iOS

下面的代码示例演示 iOS 上的 IDeviceOrientationService 接口实现:

namespace DependencyServiceDemos.iOS
{
    public class DeviceOrientationService : IDeviceOrientationService
    {
        public DeviceOrientation GetOrientation()
        {
            UIInterfaceOrientation orientation = UIApplication.SharedApplication.StatusBarOrientation;

            bool isPortrait = orientation == UIInterfaceOrientation.Portrait ||
                orientation == UIInterfaceOrientation.PortraitUpsideDown;
            return isPortrait ? DeviceOrientation.Portrait : DeviceOrientation.Landscape;
        }
    }
}

Android

下面的代码示例演示 Android 上的 IDeviceOrientationService 接口实现:

namespace DependencyServiceDemos.Droid
{
    public class DeviceOrientationService : IDeviceOrientationService
    {
        public DeviceOrientation GetOrientation()
        {
            IWindowManager windowManager = Android.App.Application.Context.GetSystemService(Context.WindowService).JavaCast<IWindowManager>();

            SurfaceOrientation orientation = windowManager.DefaultDisplay.Rotation;
            bool isLandscape = orientation == SurfaceOrientation.Rotation90 ||
                orientation == SurfaceOrientation.Rotation270;
            return isLandscape ? DeviceOrientation.Landscape : DeviceOrientation.Portrait;
        }
    }
}

通用 Windows 平台

以下代码示例展示了通用 Windows 平台 (UWP) 上的 IDeviceOrientationService 接口实现:

namespace DependencyServiceDemos.UWP
{
    public class DeviceOrientationService : IDeviceOrientationService
    {
        public DeviceOrientation GetOrientation()
        {
            ApplicationViewOrientation orientation = ApplicationView.GetForCurrentView().Orientation;
            return orientation == ApplicationViewOrientation.Landscape ? DeviceOrientation.Landscape : DeviceOrientation.Portrait;
        }
    }
}

注册平台实现

在各个平台项目中实现此接口后,必须通过 DependencyService 注册平台实现,Xamarin.Forms 才可以在运行时找到它们。 通常使用 DependencyAttribute 完成此操作,它指示指定的类型提供接口实现。

下面的示例演示如何使用 DependencyAttribute 注册 IDeviceOrientationService 接口的 iOS 实现:

using Xamarin.Forms;

[assembly: Dependency(typeof(DependencyServiceDemos.iOS.DeviceOrientationService))]
namespace DependencyServiceDemos.iOS
{
    public class DeviceOrientationService : IDeviceOrientationService
    {
        public DeviceOrientation GetOrientation()
        {
            ...
        }
    }
}

在此示例中,DependencyAttribute 通过 DependencyService 注册 DeviceOrientationService。 同样,其他平台上的 IDeviceOrientationService 接口的实现也应通过 DependencyAttribute 注册。

有关通过 DependencyService 注册平台实现的详细信息,请参阅 Xamarin.Forms DependencyService 注册和解析

解析平台实现

通过 DependencyService 注册平台实现后,必须先解析实现,才能调用它们。 通常使用 DependencyService.Get<T> 方法在共享代码中完成此操作。

下面的代码示例说明了如何调用 Get<T> 方法解析 IDeviceOrientationService 接口并调用 GetOrientation 方法:

IDeviceOrientationService service = DependencyService.Get<IDeviceOrientationService>();
DeviceOrientation orientation = service.GetOrientation();

也可以将此代码压缩到一行:

DeviceOrientation orientation = DependencyService.Get<IDeviceOrientationService>().GetOrientation();

有关通过 DependencyService 解析平台实现的详细信息,请参阅 Xamarin.Forms DependencyService 注册和解析