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()
        {
            ...
        }
    }
}

在此範例中,DependencyAttributeDependencyService 登錄 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 註冊和解析