Share via


.NET MAUI 창

.NET 다중 플랫폼 앱 UI(.NET MAUI) Window 클래스는 여러 창을 만들고, 구성하고, 표시하고, 관리하는 기능을 제공합니다.

Window는 다음 속성을 정의합니다.

  • FlowDirection형식 FlowDirection의 은 창의 UI 요소가 배치되는 방향을 정의합니다.
  • Height형식 double의 Windows에서 창의 높이를 지정합니다.
  • MaximumHeight형식 double의 는 데스크톱 플랫폼에서 창의 최대 높이를 나타냅니다. 유효한 값은 0에서 double.PositiveInfinity.
  • MaximumWidth형식 double의 는 데스크톱 플랫폼에서 창의 최대 너비를 나타냅니다. 유효한 값은 0에서 double.PositiveInfinity.
  • MinimumHeight형식 double의 는 데스크톱 플랫폼에서 창의 최소 높이를 나타냅니다. 유효한 값은 0에서 double.PositiveInfinity.
  • MinimumWidth형식 double의 는 데스크톱 플랫폼에서 창의 최소 너비를 나타냅니다. 유효한 값은 0에서 double.PositiveInfinity.
  • Overlays형식 IReadOnlyCollection<IWindowOverlay>의 창 오버레이 컬렉션을 나타냅니다.
  • Page형식 Page은 창에 표시되는 페이지를 나타냅니다. 이 속성은 클래스의 Window 콘텐츠 속성이므로 명시적으로 설정할 필요가 없습니다.
  • Title형식 string의 는 창의 제목을 나타냅니다.
  • Width형식 double의 Windows에서 창의 너비를 지정합니다.
  • X형식 double의 Windows에서 창의 X 좌표를 지정합니다.
  • Y형식 double의 Windows에서 창의 Y 좌표를 지정합니다.

속성을 제외한 Overlays 이러한 속성은 개체에 의해 BindableProperty 지원됩니다. 즉, 데이터 바인딩의 대상이 될 수 있으며 스타일이 지정될 수 있습니다.

클래스는 Window 다음 이벤트를 정의합니다.

  • Created- 창이 만들어지면 발생합니다.
  • Resumed- 창이 절전 상태에서 다시 시작될 때 발생합니다.
  • Activated- 창이 활성화될 때 발생합니다.
  • Deactivated- 창이 비활성화될 때 발생합니다.
  • Stopped- 창이 중지될 때 발생합니다.
  • Destroying- 창이 제거될 때 발생합니다.
  • SizeChanged창 크기가 변경될 때 데스크톱 플랫폼에서 발생하는 입니다.
  • Backgrounding- 창이 닫혀 있거나 배경 상태가 되면 iOS 및 Mac Catalyst에서 발생하는 함께 제공되는 BackgroundingEventArgs 개체를 사용합니다. 이 이벤트는 개체의 BackgroundingEventArgs 속성에 State 상태를 string 유지하는 데 사용할 수 있으며, OS는 창을 다시 시작할 때까지 유지됩니다. 창을 다시 시작하면 메서드에 대한 인수 CreateWindowIActivationState 통해 상태가 제공됩니다.
  • <xref:Microsoft.Maui.Controls.WindowDisplayDensityChanged는 창에 대한 유효 DPI(인치당 점)가 변경되었을 때 Android 및 Windows에서 발생하는 함께 제공되는 DisplayDensityChangedEventArgs 개체와 함께 제공됩니다.

수명 주기 이벤트 및 관련 재정의에 대한 자세한 내용은 앱 수명 주기를 참조 하세요.

클래스는 Window 다음 모달 탐색 이벤트도 정의합니다.

  • ModalPoppedModalPoppedEventArgs- 보기가 모듈식으로 튀어나왔을 때 발생합니다.
  • ModalPoppingModalPoppingEventArgs- 보기가 모듈식으로 팝될 때 발생합니다.
  • ModalPushedModalPushedEventArgs- 뷰가 모듈식으로 푸시된 후 발생하는 입니다.
  • ModalPushing- ModalPushingEventArgs뷰를 모듈식으로 푸시할 때 발생합니다.
  • PopCanceled- 모달 팝이 취소될 때 발생합니다.

클래스에는 VisualElement 부모 Window 개체를 Window 노출하는 속성이 있습니다. 개체를 조작 Window 하기 위해 모든 페이지, 레이아웃 또는 보기에서 이 속성에 액세스할 수 있습니다.

창 만들기

기본적으로 .NET MAUI는 클래스의 WindowMainPage 개체로 속성을 설정할 때 개체 AppPage 만듭니다. 그러나 클래스의 메서드를 재정의 CreateWindow 하여 개체를 만들 수도 있습니다App.Window

namespace MyMauiApp
{
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

            MainPage = new MainPage();
        }

        protected override Window CreateWindow(IActivationState activationState)
        {
            Window window = base.CreateWindow(activationState);

            // Manipulate Window object

            return window;
        }
    }
}

클래스에는 앱의 Window 루트 페이지를 나타내는 인수를 허용하는 Page 기본 생성자 및 생성자가 있지만 기본 CreateWindow 메서드를 호출하여 .NET MAUI에서 만든 Window 개체를 반환할 수도 있습니다.

또한 고유한 Window파생 개체를 만들 수도 있습니다.

namespace MyMauiApp
{
    public class MyWindow : Window
    {
        public MyWindow() : base()
        {
        }

        public MyWindow(Page page) : base(page)
        {
        }

        // Override Window methods
    }
}

그런 다음 클래스의 재정의 Window에 개체를 MyWindow 만들어 파생 클래스를 CreateWindow 사용할 수 있습니다 App .

개체를 만드는 방법에 Window 관계없이 앱에서 루트 페이지의 부모가 됩니다.

다중 창 지원

Android, iPad의 iOS(iPadOS), Mac Catalyst 및 Windows에서 동시에 여러 창을 열 수 있습니다. 개체를 만들고 개체의 메서드 ApplicationWindow 사용하여 OpenWindow 열어서 이 작업을 수행할 수 있습니다.

Window secondWindow = new Window(new MyPage());
Application.Current.OpenWindow(secondWindow);

기본 형식 IReadOnlyList<Window> 의 컬렉션은 Application.Current.Windows 개체에 등록된 모든 Window 개체에 대한 참조를 Application 가져옵니다.

다음 방법으로 Windows를 닫을 수 있습니다.Application.Current.CloseWindow

// Close a specific window
Application.Current.CloseWindow(secondWindow);

// Close the active window
Application.Current.CloseWindow(GetParentWindow());

Important

다중 창 지원은 추가 구성 없이 Android 및 Windows에서 작동합니다. 그러나 iPadOS 및 Mac Catalyst에는 추가 구성이 필요합니다.

iPadOS 및 macOS 구성

iPadOS 및 Mac Catalyst에서 다중 창 지원을 사용하려면 Platforms iOS 및 Platforms >> MacCatalyst 폴더에 명명된 SceneDelegate클래스를 추가합니다.

using Foundation;
using Microsoft.Maui;
using UIKit;

namespace MyMauiApp;

[Register("SceneDelegate")]
public class SceneDelegate : MauiUISceneDelegate
{
}

그런 다음 XML 편집기에서 Platforms iOS > Info.plist 파일 및 Platforms > MacCatalyst > Info.plist 파일을 열고 각 파일의 끝에 다음 XML을 추가 > 합니다.

<key>UIApplicationSceneManifest</key>
<dict>
  <key>UIApplicationSupportsMultipleScenes</key>
  <true/>
  <key>UISceneConfigurations</key>
  <dict>
    <key>UIWindowSceneSessionRoleApplication</key>
    <array>
      <dict>
        <key>UISceneConfigurationName</key>
        <string>__MAUI_DEFAULT_SCENE_CONFIGURATION__</string>
        <key>UISceneDelegateClassName</key>
        <string>SceneDelegate</string>
      </dict>
    </array>
  </dict>
</dict>

Important

i전화 iOS에서는 다중 창 지원이 작동하지 않습니다.

창 위치 및 크기 조정

창의 위치와 크기는 개체의 , HeightYWidth및 속성을 설정X하여 Windows의 .NET MAUI 앱에 Window 대해 프로그래밍 방식으로 정의할 수 있습니다.

Warning

Mac Catalyst는 , 및 WidthHeight 속성을 설정하여 프로그래밍 방식으로 창의 XY크기 조정 또는 위치 조정을 지원하지 않습니다.

예를 들어 시작 시 창 위치와 크기를 설정하려면 클래스의 메서드를 재정의 CreateWindow 하고 개체의 App , YWidthHeight 속성을 Window 설정X해야 합니다.

public partial class App : Application
{
    public App()
    {
        InitializeComponent();
    }

    protected override Window CreateWindow(IActivationState activationState) =>
        new Window(new AppShell())
        {
            Width = 700,
            Height = 500,
            X = 100,
            Y = 100
        };
}

또는 페이지, 레이아웃 또는 보기에서 속성에 액세스하여 Window 창을 배치하고 크기를 조정합니다. 예를 들어 다음 코드는 화면 가운데에 창을 배치하는 방법을 보여줍니다.

// Get display size
var displayInfo = DeviceDisplay.Current.MainDisplayInfo;

// Center the window
Window.X = (displayInfo.Width / displayInfo.Density - Window.Width) / 2;
Window.Y = (displayInfo.Height / displayInfo.Density - Window.Height) / 2;

디바이스의 화면 메트릭을 가져오는 방법에 대한 자세한 내용은 디바이스 표시 정보를 참조하세요.

Mac Catalyst

Mac Catalyst는 프로그래밍 방식으로 창 크기 조정 또는 위치 조정을 지원하지 않습니다. 그러나 크기 조정을 사용하도록 설정하는 해결 방법은 창의 MinimumWidth 원하는 너비 및 속성과 MaximumWidthMinimumHeightMaximumHeight 속성을 원하는 창 높이로 설정하는 것입니다. 이렇게 하면 크기 조정이 트리거되고 속성을 원래 값으로 다시 되돌리기 수 있습니다.

Window.MinimumWidth = 700;
Window.MaximumWidth = 700;
Window.MinimumHeight = 500;
Window.MaximumHeight = 500;

// Give the Window time to resize
Dispatcher.Dispatch(() =>
{
    Window.MinimumWidth = 0;
    Window.MinimumHeight = 0;
    Window.MaximumWidth = double.PositiveInfinity;
    Window.MaximumHeight = double.PositiveInfinity;
});

앱 클래스에서 창 관리 분리

인터페이스를 구현 IWindowCreator 하는 클래스를 App 만들고 메서드에 창 관리 코드를 CreateWindow 추가하여 클래스에서 창 관리를 분리할 수 있습니다.

public class WindowCreator : IWindowCreator
{
    public Window CreateWindow(Application app, IActivationState activationState)
    {
        var window = new Window(new ContentPage
        {
            Content = new Grid
            {
                new Label
                {
                    Text = "Hello from IWindowCreator",
                    HorizontalOptions = LayoutOptions.Center,
                    VerticalOptions = LayoutOptions.Center
                }
            }
        });

        return window;
    }
}

그런 다음 클래스에서 MauiProgram 창 관리 유형을 앱의 서비스 컨테이너에 종속성으로 등록해야 합니다.

builder.Services.AddSingleton<IWindowCreator, WindowCreator>();

Important

등록 코드가 인터페이스와 구체적인 형식을 지정 IWindowCreator 하는지 확인합니다.

그런 다음 클래스가 속성을 설정하지 않는지 확인합니다 AppMainPage .

public partial class App : Application
{
    public App()
    {
        InitializeComponent();
    }
}

인터페이스 및 구체적인 형식이 IWindowCreator 앱의 서비스 컨테이너에 등록되어 있고 클래스의 Application 속성이 MainPage 설정되지 않은 경우 등록된 형식을 사용하여 만듭니Window다.