앱 사전 실행 처리

OnLaunched 메서드를 재정의하고 CoreApplication.EnablePrelaunch를 호출해서 앱 사전 시작 작업을 처리하는 방법을 알아보세요.

소개

사용 가능한 시스템 리소스가 허용되는 경우 백그라운드에서 사용자가 가장 자주 사용하는 앱을 적극적으로 시작하여 데스크톱 디바이스 제품군 디바이스에서 UWP 앱의 시작 성능이 개선됩니다. 사전 시작된 앱은 시작 직후 일시 중단된 상태로 전환됩니다. 그런 다음 사용자가 앱을 호출하면, 앱이 일시 중단된 상태에서 실행 중 상태로 변경되면서 다시 시작됩니다. 이는 앱 콜드를 시작하는 것보다 빠릅니다. 이 사용자의 환경은 앱이 매우 빠르게 시작되었다는 것입니다.

Windows 10 이전에는 앱들이 사전 시작 기능을 자동으로 사용하지 않았습니다. Windows 10 버전 1511에서는 모든 UWP(유니버설 Windows 플랫폼) 앱이 사전 시작 기능의 후보였습니다. Windows 10 버전 1607에서는 CoreApplication.EnablePrelaunch를 호출하고 true을(를) 전달해서 사전 시작 동작을 옵트인해야 합니다. 이 호출을 하기 적합한 장소는 if (e.PrelaunchActivated == false) 검사가 이뤄진 위치 근처의 OnLaunched 내에 있습니다.

앱의 사전 시작 여부는 시스템 리소스에 따라 달라집니다. 시스템에 리소스 부담이 발생하면 앱이 사전 시작되지 않습니다.

일부 유형의 앱은 사전 시작이 잘 작동할 수 있도록 시작 동작을 변경해야 할 수 있습니다. 예를 들면 어떤 앱은 시작 시 음악을 재생하고, 어떤 게임은 사용자가 있다는 가정 하에 앱 시작 시 정교한 시각 효과를 보여주며, 어떤 메시징 앱은 시작 도중에 사용자의 온라인 가시성을 변경합니다. 이 모든 앱은 앱이 사전 시작된 시기를 식별할 수 있으며 아래 섹션에 설명된 대로 시작 동작을 변경할 수 있습니다.

XAML 프로젝트(C#, VB, C++)의 기본 템플릿은 사전 시작이 가능합니다.

사전 시작 및 앱 수명 주기

앱은 사전 시작된 후에 일시 중단 상태가 됩니다. (앱 일시 중단 처리 참조).

사전 시작 감지 및 처리

앱은 활성화 중에 LaunchActivatedEventArgs.PrelaunchActivated 플래그를 수신합니다. 이 플래그를 사용하여 Application.OnLaunched에 대한 다음 수정 사항과 같이 사용자가 명시적으로 앱을 시작할 때에만 실행해야 하는 코드를 실행합니다.

protected override void OnLaunched(LaunchActivatedEventArgs e)
{
    // CoreApplication.EnablePrelaunch was introduced in Windows 10 version 1607
    bool canEnablePrelaunch = Windows.Foundation.Metadata.ApiInformation.IsMethodPresent("Windows.ApplicationModel.Core.CoreApplication", "EnablePrelaunch");

    // NOTE: Only enable this code if you are targeting a version of Windows 10 prior to version 1607,
    // and you want to opt out of prelaunch.
    // In Windows 10 version 1511, all UWP apps were candidates for prelaunch.
    // Starting in Windows 10 version 1607, the app must opt in to be prelaunched.
    //if ( !canEnablePrelaunch && e.PrelaunchActivated == true)
    //{
    //    return;
    //}

    Frame rootFrame = Window.Current.Content as Frame;

    // Do not repeat app initialization when the Window already has content,
    // just ensure that the window is active
    if (rootFrame == null)
    {
        // Create a Frame to act as the navigation context and navigate to the first page
        rootFrame = new Frame();

        rootFrame.NavigationFailed += OnNavigationFailed;

        if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
        {
            //TODO: Load state from previously suspended application
        }

        // Place the frame in the current Window
        Window.Current.Content = rootFrame;
    }

    if (e.PrelaunchActivated == false)
    {
        // On Windows 10 version 1607 or later, this code signals that this app wants to participate in prelaunch
        if (canEnablePrelaunch)
        {
            TryEnablePrelaunch();
        }

        // TODO: This is not a prelaunch activation. Perform operations which
        // assume that the user explicitly launched the app such as updating
        // the online presence of the user on a social network, updating a
        // what's new feed, etc.

        if (rootFrame.Content == null)
        {
            // When the navigation stack isn't restored navigate to the first page,
            // configuring the new page by passing required information as a navigation
            // parameter
            rootFrame.Navigate(typeof(MainPage), e.Arguments);
        }
        // Ensure the current window is active
        Window.Current.Activate();
    }
}

/// <summary>
/// This method should be called only when the caller
/// determines that we're running on a system that
/// supports CoreApplication.EnablePrelaunch.
/// </summary>
private void TryEnablePrelaunch()
{
    Windows.ApplicationModel.Core.CoreApplication.EnablePrelaunch(true);
}

Important

위 코드 예제에서 TryEnablePrelaunch 메서드는 CoreApplication.EnablePrelaunch를 호출합니다. 또한 TryEnablePrelaunch는 앱이 CoreApplication.EnablePrelaunch를 지원하는 Windows 버전에서 실행되는 경우에만 자체적으로 호출됩니다. 의심의 여지가 있는 경우, 일반적으로는 코드가 실행 중인 플랫폼에서 지원된다는 것을 확인한 후에만 Windows API 을(를) 사용해야 합니다. 이 작업은 위 코드 예제에 나온 대로 ApiInformation 클래스를 통해 수행할 수 있습니다.

위 예제에는 앱이 Windows 10 버전 1511에서 실행될 때 사전 시작을 옵트아웃해야 하는 경우 주석 처리를 제거할 수 있는 코드도 있습니다. 버전 1511에서는 모든 UWP 앱이 사전 시작되도록 자동으로 옵트인되었는데, 이는 해당 앱에 적합하지 않을 수 있습니다.

VisibilityChanged 이벤트 사용

사전 시작으로 활성화된 앱은 사용자에게 표시되지 않습니다. 이러한 앱은 사용자가 해당 앱으로 전환할 때 표시됩니다. 앱의 기본 창이 표시될 때까지 특정 작업을 지연시킬 수 있습니다. 그 예로 앱이 피드의 새 항목 목록을 표시하는 경우, 사용자가 해당 앱을 활성화할 때가 되면 목록의 기한이 오래될 수 있으므로, 앱이 사전 시작되었을 때 빌드된 목록을 사용하지 않고 VisibilityChanged 이벤트 중에 목록을 업데이트할 수 있습니다. 다음 코드는 MainPage에 대한 VisibilityChanged 이벤트를 처리합니다.

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();

        Window.Current.VisibilityChanged += WindowVisibilityChangedEventHandler;
    }

    void WindowVisibilityChangedEventHandler(System.Object sender, Windows.UI.Core.VisibilityChangedEventArgs e)
    {
        // Perform operations that should take place when the application becomes visible rather than
        // when it is prelaunched, such as building a what's new feed
    }
}

DirectX 게임 지침

상당수의 DirectX 게임은 사전 시작이 감지되기 전에 초기화를 수행합니다. 따라서 DirectX 게임은 일반적으로 사전 시작 기능을 사용하도록 설정하지 않아야 합니다. Windows 1607 Anniversary Edition부터는 게임이 기본적으로 사전 시작되지 않습니다. 게임에서 사전 시작 기능을 사용하려면 CoreApplication.EnablePrelaunch(true)를 호출하세요.

게임이 이전 버전의 Windows 10을 대상으로 하는 경우, 사전 시작 조건을 처리하여 애플리케이션을 종료하면 됩니다.

void ViewProvider::OnActivated(CoreApplicationView const& /* appView */, Windows::ApplicationModel::Activation::IActivatedEventArgs const& args)
{
    if (args.Kind() == Windows::ApplicationModel::Activation::ActivationKind::Launch)
    {
        auto launchArgs{ args.as<Windows::ApplicationModel::Activation::LaunchActivatedEventArgs>()};
        if (launchArgs.PrelaunchActivated())
        {
            // Opt-out of Prelaunch.
            CoreApplication::Exit();
        }
    }
}

void ViewProvider::Initialize(CoreApplicationView const & appView)
{
    appView.Activated({ this, &App::OnActivated });
}
void ViewProvider::OnActivated(CoreApplicationView^ appView,IActivatedEventArgs^ args)
{
    if (args->Kind == ActivationKind::Launch)
    {
        auto launchArgs = static_cast<LaunchActivatedEventArgs^>(args);
        if (launchArgs->PrelaunchActivated)
        {
            // Opt-out of Prelaunch
            CoreApplication::Exit();
            return;
        }
    }
}

일반 지침

  • 앱은 신속하게 일시 중단될 수 없는 경우 종료됩니다. 따라서 사전 시작 도중에는 앱이 장기 작업을 실행해서는 안 됩니다.
  • 앱이 표시되지 않고 오디오 재생이 있는 이유가 명백하지 않습니다. 따라서 앱이 사전 시작될 때는 앱의 Application.OnLaunched에서 오디오 재생을 시작해서는 안 됩니다.
  • 앱은 시작(앱이 사용자에게 표시된다고 가정하거나, 사용자가 앱을 명시적으로 시작했다고 가정함) 도중에 어떤 작업도 실행해서는 안 됩니다. 이제 명시적인 사용자 작업 없이 백그라운드에서 앱을 시작할 수 있으므로, 개발자는 개인 정보 보호, 사용자 환경 및 성능에 미치는 영향을 고려해야 합니다.
    • 개인 정보 보호를 고려하는 상황의 예는 소셜 앱이 사용자 상태를 온라인으로 변경해야 하는 경우입니다. 앱이 사전 시작될 때는 상태를 변경하는 대신 사용자가 해당 앱으로 전환할 때까지 기다려야 합니다.
    • 사용자 환경을 고려하는 상황의 예는 시작 시 도입부 시퀀스를 표시하는 앱(예: 게임)이 출시될 경우, 사용자가 해당 앱으로 전환할 때까지 도입부 시퀀스를 지연시킬 수 있는 경우입니다.
    • 성능에 미치는 영향의 예는 앱이 사전 시작될 때 사용자가 앱을 로드하는 대신 현재 날씨 정보를 검색하기 위해 해당 앱으로 전환할 때까지 기다린 다음, 앱이 표시될 때 해당 앱을 다시 로드하여 정보가 최신 상태인지 확인해야 하는 경우입니다.
  • 앱이 시작될 때 Live Tile을 지우면 가시성이 이벤트를 변경할 때까지 이 작업의 수행을 연기하세요.
  • 앱에 대한 원격 분석에서는 문제가 발생할 경우 시나리오를 보다 쉽게 좁힐 수 있도록 일반적인 타일 활성화와 사전 시작 활성화를 구분해야 합니다.
  • Microsoft Visual Studio 2015 업데이트 1 및 Windows 10 버전 1511이 있는 경우 디버그>, 다른 디버그의 대상>, 디버그 Windows 유니버설 앱 사전 시작을 선택해서 Visual Studio 2015에서 앱의 사전 시작을 시뮬레이션할 수 있습니다.