플랫폼 코드 호출

샘플을 찾아봅니다. 샘플 찾아보기

.NET 다중 플랫폼 앱 UI(.NET MAUI)가 특정 플랫폼 API에 액세스하기 위한 API를 제공하지 않는 경우 필요한 플랫폼 API에 액세스하는 고유한 코드를 작성할 수 있습니다. 이를 위해서는 Apple의 iOS 및 MacCatalyst API, Google의 Android APIMicrosoft의 Windows 앱 SDK API에 대한 지식이 필요합니다.

플랫폼 코드는 조건부 컴파일을 사용하거나 부분 클래스 및 부분 메서드를 사용하여 플랫폼 간 코드에서 호출할 수 있습니다.

조건부 컴파일

조건부 컴파일을 사용하여 여러 플랫폼을 대상으로 하여 플랫폼 간 코드에서 플랫폼 코드를 호출할 수 있습니다.

다음 예제에서는 디바이스의 DeviceOrientation 방향을 지정하는 데 사용되는 열거형을 보여 줍니다.

namespace InvokePlatformCodeDemos.Services
{
    public enum DeviceOrientation
    {
        Undefined,
        Landscape,
        Portrait
    }
}

디바이스 방향을 검색하려면 플랫폼 코드를 작성해야 합니다. 이 작업은 조건부 컴파일을 사용하여 다른 플랫폼을 대상으로 하는 메서드를 작성하여 수행할 수 있습니다.

#if ANDROID
using Android.Content;
using Android.Views;
using Android.Runtime;
#elif IOS
using UIKit;
#endif

using InvokePlatformCodeDemos.Services;

namespace InvokePlatformCodeDemos.Services.ConditionalCompilation
{
    public class DeviceOrientationService
    {
        public DeviceOrientation GetOrientation()
        {
#if ANDROID
            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;
#elif IOS
            UIInterfaceOrientation orientation = UIApplication.SharedApplication.StatusBarOrientation;
            bool isPortrait = orientation == UIInterfaceOrientation.Portrait || orientation == UIInterfaceOrientation.PortraitUpsideDown;
            return isPortrait ? DeviceOrientation.Portrait : DeviceOrientation.Landscape;
#else
            return DeviceOrientation.Undefined;
#endif
        }
    }
}

이 예제에서는 Android 및 iOS용으로 메서드의 GetOrientation 플랫폼 구현이 제공됩니다. 다른 플랫폼에서 DeviceOrientation.Undefined 는 반환됩니다. 또는 반환 DeviceOrientation.Undefined 하는 대신 구현이 제공되는 플랫폼을 지정하는 플랫폼을 throw PlatformNotSupportedException 할 수 있습니다.

throw new PlatformNotSupportedException("GetOrientation is only supported on Android and iOS.");

DeviceOrientationService.GetOrientation 그런 다음 개체 인스턴스를 만들고 해당 작업을 호출하여 플랫폼 간 코드에서 메서드를 호출할 수 있습니다.

using InvokePlatformCodeDemos.Services;
using InvokePlatformCodeDemos.Services.ConditionalCompilation;
...

DeviceOrientationService deviceOrientationService = new DeviceOrientationService();
DeviceOrientation orientation = deviceOrientationService.GetOrientation();

빌드 시 빌드 시스템은 조건부 컴파일을 사용하여 Android 및 iOS 플랫폼 코드를 올바른 플랫폼으로 대상으로 지정합니다.

조건부 컴파일에 대한 자세한 내용은 조건부 컴파일을 참조하세요.

부분 클래스 및 메서드

.NET MAUI 앱 프로젝트에는 .NET MAUI가 대상으로 지정할 수 있는 플랫폼을 나타내는 각 자식 폴더가 있는 Platforms 폴더가 포함됩니다.

플랫폼 폴더 스크린샷

각 대상 플랫폼의 폴더에는 각 플랫폼에서 앱을 시작하는 플랫폼별 코드와 추가하는 추가 플랫폼 코드가 포함됩니다. 빌드 시 빌드 시스템에는 특정 플랫폼을 빌드할 때 각 폴더의 코드만 포함됩니다. 예를 들어 Android용으로 빌드할 때 Platforms>Android 폴더의 파일은 앱 패키지에 기본 제공되지만 다른 Platforms 폴더의 파일은 빌드되지 않습니다. 이 방법은 다중 대상 지정이라는 기능을 사용하여 단일 프로젝트에서 여러 플랫폼을 대상으로 합니다.

다중 대상 지정을 부분 클래스 및 부분 메서드와 결합하여 플랫폼 간 코드에서 플랫폼 기능을 호출할 수 있습니다. 이 작업을 수행하는 프로세스는 다음과 같습니다.

  1. 플랫폼 간 API를 각 플랫폼에서 호출하려는 모든 작업에 대한 부분 메서드 서명을 정의하는 부분 클래스로 정의합니다. 자세한 내용은 플랫폼 간 API 정의를 참조하세요.
  2. 동일한 부분 클래스와 동일한 부분 메서드 서명을 정의하고 메서드 구현을 제공하여 플랫폼당 플랫폼 간 API를 구현합니다. 자세한 내용은 플랫폼당 API 구현을 참조하세요.
  3. 부분 클래스의 인스턴스를 만들고 필요에 따라 해당 메서드를 호출하여 플랫폼 간 API를 호출합니다. 자세한 내용은 플랫폼 간 API 호출을 참조하세요.

플랫폼 간 API 정의

플랫폼 간 코드에서 플랫폼 코드를 호출하기 위해 첫 번째 단계는 플랫폼 간 API를 각 플랫폼에서 호출하려는 모든 작업에 대한 부분 메서드 서명을 정의하는 부분 클래스로 정의하는 것입니다.

다음 예제에서는 디바이스의 DeviceOrientation 방향을 지정하는 데 사용되는 열거형을 보여 줍니다.

namespace InvokePlatformCodeDemos.Services
{
    public enum DeviceOrientation
    {
        Undefined,
        Landscape,
        Portrait
    }
}

다음 예제에서는 디바이스의 방향을 검색하는 데 사용할 수 있는 플랫폼 간 API를 보여줍니다.

namespace InvokePlatformCodeDemos.Services.PartialMethods
{
    public partial class DeviceOrientationService
    {
        public partial DeviceOrientation GetOrientation();
    }
}

partial 클래스의 이름은 DeviceOrientationService부분 메서드 GetOrientation를 포함합니다. 이 클래스의 코드 파일은 Platforms 폴더 외부에 있어야 합니다.

Services 폴더 스크린샷의 DeviceOrientationService 클래스입니다.

플랫폼당 API 구현

플랫폼 간 API를 정의한 후에는 동일한 부분 클래스와 동일한 부분 메서드 시그니처를 정의하고 메서드 구현을 제공하여 대상으로 지정하는 모든 플랫폼에서 구현해야 합니다.

플랫폼 구현은 빌드 시스템이 특정 플랫폼용으로 빌드할 때만 플랫폼 코드를 빌드하려고 시도하도록 올바른 Platforms 자식 폴더에 배치해야 합니다. 다음 표에서는 플랫폼 구현에 대한 기본 폴더 위치를 나열합니다.

플랫폼 폴더
Android 플랫폼>안 드 로이드
iOS 플랫폼>Ios
MacCatalyst 플랫폼>MacCatalyst
Tizen 플랫폼>Tizen
Windows 플랫폼>Windows

중요

플랫폼 구현은 플랫폼 간 API가 정의한 것과 동일한 네임스페이스 및 동일한 클래스에 있어야 합니다.

다음 스크린샷은 AndroidiOS 폴더의 클래스를 보여 DeviceOrientationService 줍니다.

Platforms 폴더의 DeviceOrientationService 클래스 스크린샷

또는 Platforms 폴더를 사용하는 대신 사용자 고유의 파일 이름 및 폴더 조건에 따라 다중 대상 지정을 수행할 수 있습니다. 자세한 내용은 다중 대상 구성을 참조하세요.

Android

다음 예제에서는 Android에서 메서드의 구현을 GetOrientation 보여줍니다.

using Android.Content;
using Android.Runtime;
using Android.Views;

namespace InvokePlatformCodeDemos.Services.PartialMethods;

public partial class DeviceOrientationService
{
    public partial 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;
    }
}

iOS

다음 예제에서는 iOS에서 메서드의 구현을 GetOrientation 보여줍니다.

using UIKit;

namespace InvokePlatformCodeDemos.Services.PartialMethods;

public partial class DeviceOrientationService
{
    public partial DeviceOrientation GetOrientation()
    {
        UIInterfaceOrientation orientation = UIApplication.SharedApplication.StatusBarOrientation;
        bool isPortrait = orientation == UIInterfaceOrientation.Portrait || orientation == UIInterfaceOrientation.PortraitUpsideDown;
        return isPortrait ? DeviceOrientation.Portrait : DeviceOrientation.Landscape;
    }
}

플랫폼 간 API 호출

플랫폼 구현을 제공한 후 개체 인스턴스를 만들고 해당 작업을 호출하여 플랫폼 간 코드에서 API를 호출할 수 있습니다.

using InvokePlatformCodeDemos.Services;
using InvokePlatformCodeDemos.Services.PartialMethods;
...

DeviceOrientationService deviceOrientationService = new DeviceOrientationService();
DeviceOrientation orientation = deviceOrientationService.GetOrientation();

빌드 시 빌드 시스템은 다중 대상을 사용하여 플랫폼 간 부분 클래스를 대상 플랫폼에 대한 부분 클래스와 결합하고 앱 패키지에 빌드합니다.

다중 대상 지정 구성

.NET MAUI 앱은 사용자 고유의 파일 이름 및 폴더 조건에 따라 다중 대상을 지정할 수도 있습니다. 이렇게 하면 플랫폼 폴더의 자식 폴더에 플랫폼 코드를 배치할 필요가 없도록 .NET MAUI 앱 프로젝트를 구성할 수 있습니다.

예를 들어 표준 다중 대상 지정 패턴은 플랫폼을 플랫폼 코드의 파일 이름에 확장명으로 포함하는 것입니다. 다음 패턴에 따라 플랫폼 간 부분 클래스를 플랫폼 부분 클래스와 결합하도록 빌드 시스템을 구성할 수 있습니다.

파일 이름 기반 다중 대상 지정을 사용하는 DeviceOrientationService 클래스입니다.

또 다른 표준 다중 대상 지정 패턴은 플랫폼을 폴더 이름으로 포함하는 것입니다. 다음 패턴에 따라 플랫폼 간 부분 클래스를 플랫폼 부분 클래스와 결합하도록 빌드 시스템을 구성할 수 있습니다.

폴더 기반 다중 대상 지정을 사용하는 DeviceOrientationService 클래스입니다.

자세한 내용은 다중 대상 구성을 참조하세요.