處理應用程式預先啟動

了解如何透過覆寫 OnLaunched 方法並呼叫 CoreApplication.EnablePrelaunch 來處理應用程式預先啟動。

簡介

當可用的系統資源允許時,在傳統型裝置系列裝置裝置上啟動UWP應用程式的啟動效能會藉由主動啟動背景中最常使用的應用程式來改善。 預啟動的應用程式在啟動後不久就會進入暫停狀態。 然後,當使用者呼叫應用程式時,應用程式將從掛起狀態恢復到執行狀態,這比冷啟動應用程式更快。 用戶的經驗是,應用程式只會非常快速地啟動。

在 Windows 10 之前,應用程式不會自動利用預先啟動。 在 Windows 10 版本 1511 中,所有通用 Windows 平台 (UWP) 應用程式都是預先啟動的候選者。 在 Windows 10 版本 1607 中,您必須呼叫 CoreApplication.EnablePrelaunch 並傳遞 true,選擇加入預先啟動行為。 放置此呼叫的好位置是在 OnLaunched 內,靠近檢查的位置 if (e.PrelaunchActivated == false)

應用程式是否預啟動取決於系統資源。 如果系統遇到資源壓力,則不會預先啟動應用程式。

某些類型的應用程式可能需要更改其啟動行為才能在預先啟動中正常運作。 例如,啟動時播放音樂的應用程式;一款假設用戶在場的遊戲,並在應用程式啟動時顯示精緻的視覺效果;在啟動期間更改用戶在線可見性的消息傳遞應用程式 - 所有這些都可以識別應用程式何時預先啟動,並且可以更改其啟動行為,如下文所述。

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。 而且只有在支援 CoreApplication.EnablePrelaunch 的 Windows 版本上執行應用程式時,才會呼叫 TryEnablePrelaunch。 一般來說,如果有任何疑問,那麼您應該僅在確定程式碼執行的平台支援 Windows API 後才使用它。 您也可以使用 ApiInformation 類別來執行此動作,如上述程式代碼範例所示。

如果您的應用程式在 Windows 10 版本 1511 上執行時需要選擇退出預啟動,則可以取消註解上面範例中的程式碼。 在版本 1511 中,所有 UWP 應用程式都會自動選擇預啟動,這可能不適合你的應用程式。

使用 VisibilityChanged 事件

使用者看不到由預先啟動所啟動的應用程式。 當使用者切換至它們時,它們就會變成可見。 您可能會想要延遲某些作業,直到您的應用程式主視窗變成可見為止。 例如,如果您的 app 顯示摘要中新增項目的清單,您可以在 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 年度版開始,您的遊戲預設不會預先啟動。 如果您要讓遊戲利用預先啟動,請呼叫 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 起始音訊播放,因為應用程式不會顯示,而且不會明顯為什麼有音訊播放。
  • 應用程式不應該在啟動期間執行任何作業,假設使用者可以看到應用程式,或假設應用程式是由使用者明確啟動。 因為應用程式現在可以在背景啟動,而不需要明確的使用者動作,開發人員應該考慮隱私權、用戶體驗和效能影響。
    • 隱私權考慮的範例是社交應用程式應該將用戶狀態變更為線上。 它應該等到使用者切換至應用程式,而不是在預先啟動應用程式時變更狀態。
    • 使用者體驗考慮的一個例子是,如果您有一個應用程式 (例如遊戲) 在啟動時顯示介紹性序列,則您可以延遲介紹性序列,直到使用者切換到該應用程式。
    • 性能影響的範例是,您可能會等到用戶切換到應用程式來擷取當前天氣信息,而不是在應用程式預啟動時載入,然後需要在應用程式變得可見時再次加載它以確保資訊是最新的。
  • 如果您的應用程式在啟動時清除了其動態磚,請延遲執行此操作,直到可見性變更事件。
  • 應用程式的遙測應區分正常磚啟動和預啟動,以便在出現問題時更輕鬆地縮小場景範圍。
  • 如果您有 Microsoft Visual Studio 2015 Update 1 和 Windows 10 版本 1511,則可以透過選擇偵錯>其他偵錯>目標和偵錯 Windows 通用應用程式預先啟動來模擬 Visual Studio 2015 中應用程式的預先啟動。