앱 사전 실행 처리

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);
}

중요

위의 코드 예제에서 TryEnablePrelaunch 메서드는 CoreApplication.EnablePrelaunch를 호출합니다. 또한 TryEnablePrelaunch는 앱이 CoreApplication.EnablePrelaunch를 지원하는 Windows 버전에서 실행되는 경우에만 호출됩니다. 일반적으로 의심의 여지가 있는 경우 코드가 실행되는 플랫폼에서 지원되는지 결정한 후에만 Windows API를 사용해야 합니다. 위의 코드 예제와 같이 ApiInformation 클래스를 통해 수행할 수 있습니다.

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

VisibilityChanged 이벤트 사용

사전 실행으로 활성화된 앱이 사용자에게 표시되지 않습니다. 사용자가 전환할 때 표시됩니다. 앱의 주 창이 표시될 때까지 특정 작업을 지연시킬 수 있습니다. 예를 들어 앱이 피드에서 새 항목 목록을 표시하면 앱이 사전 실행되었을 때 작성되어 사용자가 앱을 활성화하는 시점까지 상태가 오래될 수 있기 때문에 목록을 사용하는 대신 VisibilityChanged 이벤트가 발생하는 동안 목록을 업데이트할 수 있습니다. 다음 코드는 MainPageVisibilityChanged 이벤트를 처리합니다.

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 1주년 버전부터 게임은 기본적으로 사전 실행되지 않습니다. 게임에 사전 실행을 활용하려면 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의 오디오 재생을 시작하면 안 됩니다.
  • 사용자에게 표시되거나 명시적으로 사용자가 시작했다고 가정하는 앱은 시작하는 동안 어떤 작업도 수행하면 안 됩니다. 앱이 현재 명시적인 사용자 작업 없이 백그라운드에서 시작되었기 때문에 개발자는 개인 정보, 사용자 환경 및 성능 영향을 고려해야 합니다.
    • 개인 정보가 고려되어야 하는 예는 소셜 앱이 사용자의 상태를 온라인으로 변경해야 하는 경우입니다. 앱이 사전 실행되면 상태를 변경하는 대신 사용자가 앱으로 전환할 때까지 기다려야 합니다.
    • 사용자 환경이 고려되는 예는 시작할 때 소개 시퀀스를 표시하는 게임과 같은 앱의 경우 사용자가 앱으로 전환할 때까지 소개 시퀀스를 지연시킬 수 있는 경우입니다.
    • 성능 영향의 예는 앱이 사전 실행될 때 현재 날씨 정보를 로드하는 대신 사용자가 앱으로 전환하여 이 정보를 검색한 다음 앱이 표시되면 다시 로드하여 해당 정보가 최신 정보인지 확인해야 하는 경우입니다.
  • 앱이 시작되었을 때 라이브 타일을 지우면 VisibilityChanged 이벤트가 발생할 때까지 이를 연기합니다.
  • 앱에 대한 원격 분석으로 일반적인 타일 활성화와 사전 실행된 활성화를 구별하여 문제 발생 시 시나리오의 범위를 쉽게 좁힐 수 있습니다.
  • Microsoft Visual Studio 2015 업데이트 1 및 버전 1511 Windows 10 경우 Visual Studio 2015에서 DebugOther>Debug TargetsDebug>Windows 유니버설 앱 PreLaunch를 선택하여 앱에 대한 사전 실행 작업을 시뮬레이션할 수 있습니다.