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 によって DeviceOrientationServiceDependencyService に登録しています。 同様に、DependencyAttribute を使用して他のプラットフォーム上の IDeviceOrientationService インターフェイスの実装を登録する必要があります。

DependencyService へのプラットフォームの実装の登録の詳細については、「Xamarin.Forms の DependencyService の登録と解決」をご覧ください。

プラットフォームの実装の解決

DependencyService にプラットフォームの実装を登録した後、呼び出される前にその実装を解決する必要があります。 これは一般に、DependencyService.Get<T> メソッドを使用して共有コード内で実行されます。

次のコードは、Get<T> メソッドを呼び出して IDeviceOrientationService インターフェイスを解決してから、その GetOrientation メソッドを呼び出す例を示しています。

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

または、このコードは、1 行に圧縮できます。

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

DependencyService でのプラットフォームの実装の解決の詳細については、「Xamarin.Forms の DependencyService の登録と解決」をご覧ください。