앱 작업

Browse sample. 샘플 찾아보기

이 문서에서는 앱 바로 가기를 만들고 응답할 수 있는 .NET 다중 플랫폼 앱 UI(.NET MAUI) IAppActions 인터페이스를 사용하는 방법을 설명합니다. 앱 바로 가기는 앱 개발자가 앱을 시작하는 추가 방법을 제시할 수 있기 때문에 사용자에게 유용합니다. 예를 들어 전자 메일 및 일정 앱을 개발하는 경우 다른 두 가지 앱 작업을 표시할 수 있습니다. 하나는 앱을 일정의 현재 날짜로 직접 열고 다른 하나는 전자 메일 받은 편지함 폴더를 여는 것입니다.

인터페이스의 IAppActions 기본 구현은 속성을 통해 AppActions.Current 사용할 수 있습니다. IAppActions 인터페이스와 AppActions 클래스는 모두 네임스페이스에 Microsoft.Maui.ApplicationModel 포함됩니다.

시작하기

기능에 액세스 AppActions 하려면 다음 플랫폼별 설정이 필요합니다.

파일에서 Platforms/Android/MainActivity.cs 클래스 및 OnNewIntent 재정의 OnResumeMainActivity 추가하고 다음 IntentFilter 특성을 추가합니다.

[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
[IntentFilter(new[] { Platform.Intent.ActionAppAction },
              Categories = new[] { global::Android.Content.Intent.CategoryDefault })]
public class MainActivity : MauiAppCompatActivity {

    protected override void OnResume()
    {
        base.OnResume();

        Platform.OnResume(this);
    }

    protected override void OnNewIntent(Android.Content.Intent intent)
    {
        base.OnNewIntent(intent);

        Platform.OnNewIntent(intent);
    }

}

작업 만들기

앱 작업은 언제든지 만들 수 있지만 앱이 시작될 때 생성되는 경우가 많습니다. 앱 작업을 구성하려면 MauiProgram.cs 파일의 MauiAppBuilder개체에서 메서드를 호출 ConfigureEssentials(MauiAppBuilder, Action<IEssentialsBuilder>) 합니다. 앱 작업을 사용하도록 설정하려면 개체에 대해 IEssentialsBuilder 호출해야 하는 두 가지 방법이 있습니다.

  1. AddAppAction

    이 메서드는 작업을 만듭니다. 작업을 고유하게 식별하는 문자열과 title 사용자에게 표시되는 문자열이 필요합니다id. 필요에 따라 자막과 아이콘을 제공할 수 있습니다.

  2. OnAppAction

    이 메서드에 전달된 대리자는 앱 작업 인스턴스를 제공하면 사용자가 앱 작업을 호출할 때 호출됩니다. Id 작업의 속성을 확인하여 사용자가 시작한 앱 동작을 확인합니다.

다음 코드는 앱 시작 시 앱 작업을 구성하는 방법을 보여 줍니다.

public static MauiApp CreateMauiApp()
{
    var builder = MauiApp.CreateBuilder();

    builder
        .UseMauiApp<App>()
        .ConfigureFonts(fonts =>
        {
            fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
            fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
        })
        .ConfigureEssentials(essentials =>
        {
            essentials
                .AddAppAction("app_info", "App Info", icon: "app_info_action_icon")
                .AddAppAction("battery_info", "Battery Info")
                .OnAppAction(App.HandleAppActions);
        });

    return builder.Build();
}

작업에 응답

앱 작업이 구성OnAppAction되면 사용자가 호출하는 모든 앱 작업에 대해 메서드가 호출됩니다. 속성을 Id 사용하여 구분합니다. 다음 코드는 앱 작업을 처리하는 방법을 보여 줍니다.

public static void HandleAppActions(AppAction appAction)
{
    App.Current.Dispatcher.Dispatch(async () =>
    {
        var page = appAction.Id switch
        {
            "battery_info" => new SensorsPage(),
            "app_info" => new AppModelPage(),
            _ => default(Page)
        };

        if (page != null)
        {
            await Application.Current.MainPage.Navigation.PopToRootAsync();
            await Application.Current.MainPage.Navigation.PushAsync(page);
        }
    });
}

앱 작업이 지원되는지 확인

앱 시작 시 또는 앱을 사용하는 동안 앱 작업을 만들 때는 속성을 읽 AppActions.Current.IsSupported 어 앱 작업이 지원되는지 확인하는 검사.

시작 부트스트랩 외부에서 앱 작업 만들기

앱 작업을 만들려면 다음 메서드를 호출합니다 SetAsync .

if (AppActions.Current.IsSupported)
{
    await AppActions.Current.SetAsync(new[] { new AppAction("app_info", "App Info", icon: "app_info_action_icon"),
                                              new AppAction("battery_info", "Battery Info") });
}

앱 작업에 대한 자세한 정보

특정 버전의 운영 체제 FeatureNotSupportedException 에서 앱 작업이 지원되지 않는 경우 해당 작업이 throw됩니다.

AppAction(String, String, String, String) 생성자를 사용하여 앱 작업의 다음 측면을 설정합니다.

  • Id: 작업 탭에 응답하는 데 사용되는 고유 식별자입니다.
  • Title: 표시할 표시 제목입니다.
  • Subtitle: 제목 아래에 표시할 부제목이 지원되는 경우
  • Icon: 각 플랫폼의 해당 리소스 디렉터리에 있는 아이콘과 일치해야 합니다.

App actions on home screen.

작업 가져오기

를 호출 AppActions.Current.GetAsync하여 앱 작업의 현재 목록을 가져올 수 있습니다.