瞭解如何透過覆寫 OnLaunched 方法,以及呼叫 CoreApplication.EnablePrelaunch來處理 app 預先啟動。
簡介
當可用的系統資源允許時,在桌面設備系列的裝置上啟動 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
上述範例中有代碼,如果您的 app 需要在 Windows 10 版本 1511 上執行時取消參與預啟動,您可以取消註解。 在 1511 版中,所有 UWP 應用程式都會自動加入預先啟動,這可能不適合您的應用程式。
使用 VisibilityChanged 事件
使用者看不到透過預啟動方式啟動的應用程式。 當使用者切換至它們時,它們就會變成可見。 您可能會想要延遲某些作業,直到您的應用程式主視窗變成可見為止。 例如,如果您的 app 顯示摘要中新增專案的清單,您可以在 VisibilityChanged 事件期間更新清單,而不是使用預先啟動 app 時所建置的清單,因為它可能會在使用者啟動應用程式時過時。 下列程式代碼會處理 VisibilityChanged 事件,針對 MainPage:
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;
}
}
}
一般方針
- 應用程式不應該在預先啟動期間執行長時間執行的作業,因為如果應用程式無法快速暫停,應用程式將會終止。
- ** 當應用程式預先啟動時,apps 不應該從 Application.OnLaunched 開始音訊播放,因為應用程式不會顯示,因此不明顯為什麼會有音訊播放。
- 應用程式不應該在啟動期間執行任何作業,假設使用者可以看到應用程式,或假設應用程式是由使用者明確啟動。 因為應用程式現在可以在背景啟動,而不需要明確的使用者動作,開發人員應該考慮隱私權、用戶體驗和效能影響。
- 隱私權考慮的範例是社交應用程式應該將用戶狀態變更為在線。 它應該等到使用者切換至應用程式,而不是在預先啟動應用程式時變更狀態。
- 用戶體驗考慮的範例是,如果您有應用程式,例如遊戲,在啟動時顯示簡介序列,您可能會延遲簡介序列,直到使用者切換至應用程式為止。
- 效能暗示的範例是,您可能會等到使用者切換至應用程式以擷取目前的天氣資訊,而不是在預先啟動應用程式時載入它,然後在應用程式變成可見狀態時再次載入它,以確保資訊是最新的。
- 如果您的 app 在啟動時清除其動態磚,請延遲執行此動作,直到可見度變更事件為止。
- 應用程式的遙測應該區分一般磁磚啟用與預啟動啟用,以便在發生問題時更輕鬆地縮小範圍確定情景。
- 如果您有 Microsoft Visual Studio 2015 Update 1 和 Windows 10 版本 1511,則可以在 Visual Studio 2015 中模擬應用程式的預先啟動。只需選擇 [偵錯]>[其他偵錯目標]>偵錯 Windows 通用應用程式的預先啟動。