了解如何通过重写 Application.OnLaunched 方法来处理应用激活。
注释
有关在桌面应用中处理激活的信息,请参阅 获取打包应用的激活信息。 另请参阅 AppLifecycle — GitHub 上的丰富激活。
重写启动处理程序
激活应用程序时,不论何种原因,系统都会发送 CoreApplicationView.Activated 事件。 有关激活类型的列表,请参阅 中的 ActivationKind 枚举。
Windows.UI.Xaml.Application 类定义可以替代的方法来处理各种激活类型。 多个激活类型具有可替代的特定方法。 对于其他激活类型,请替代 onActivated 方法 。
定义应用程序的类。
x:Class="AppName.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 重写 OnLaunched 方法。 每当用户启动应用时,将调用此方法。 LaunchActivatedEventArgs 参数包含应用的先前状态和激活参数。 注释 在 Windows 上,从“开始”磁贴或应用列表中启动已挂起的应用程序不会调用此方法。 using System; using Windows.ApplicationModel.Activation; using Windows.UI.Xaml; namespace AppName { public partial class App { async protected override void OnLaunched(LaunchActivatedEventArgs args) { EnsurePageCreatedAndActivate(); } // Creates the MainPage if it isn't already created. Also activates // the window so it takes foreground and input focus. private MainPage EnsurePageCreatedAndActivate() { if (Window.Current.Content == null) { Window.Current.Content = new MainPage(); } Window.Current.Activate(); return Window.Current.Content as MainPage; } } } Class App Protected Overrides Sub OnLaunched(args As LaunchActivatedEventArgs) Window.Current.Content = New MainPage() Window.Current.Activate() End Sub End Class ... #include "MainPage.h" #include "winrt/Windows.ApplicationModel.Activation.h" #include "winrt/Windows.UI.Xaml.h" #include "winrt/Windows.UI.Xaml.Controls.h" ... using namespace winrt; using namespace Windows::ApplicationModel::Activation; using namespace Windows::UI::Xaml; using namespace Windows::UI::Xaml::Controls; struct App : AppT { App(); /// /// Invoked when the application is launched normally by the end user. Other entry points /// will be used such as when the application is launched to open a specific file. /// /// Details about the launch request and process. void OnLaunched(LaunchActivatedEventArgs const& e) { Frame rootFrame{ nullptr }; auto content = Window::Current().Content(); if (content) { rootFrame = content.try_as(); } // Do not repeat app initialization when the Window already has content, // just ensure that the window is active if (rootFrame == nullptr) { // Create a Frame to act as the navigation context and associate it with // a SuspensionManager key rootFrame = Frame(); rootFrame.NavigationFailed({ this, &App::OnNavigationFailed }); if (e.PreviousExecutionState() == ApplicationExecutionState::Terminated) { // Restore the saved session state only when appropriate, scheduling the // final launch steps after the restore is complete } if (e.PrelaunchActivated() == false) { if (rootFrame.Content() == nullptr) { // 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(xaml_typename } // Place the frame in the current Window Window::Current().Content(rootFrame); // Ensure the current window is active Window::Current().Activate(); } } else { if (e.PrelaunchActivated() == false) { if (rootFrame.Content() == nullptr) { // 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(xaml_typename } // Ensure the current window is active Window::Current().Activate(); } } } }; using namespace Windows::ApplicationModel::Activation; using namespace Windows::Foundation; using namespace Windows::UI::Xaml; using namespace AppName; void App::OnLaunched(LaunchActivatedEventArgs^ args) { EnsurePageCreatedAndActivate(); } // Creates the MainPage if it isn't already created. Also activates // the window so it takes foreground and input focus. void App::EnsurePageCreatedAndActivate() { if (_mainPage == nullptr) { // Save the MainPage for use if we get activated later _mainPage = ref new MainPage(); } Window::Current->Content = _mainPage; Window::Current->Activate(); } 如果应用程序被暂停然后终止,则恢复其数据。 当用户切换到已终止的应用时,系统发送 已激活的 事件,并将 Kind 设置为 Launch,将 PreviousExecutionState 设置为 Terminated 或 ClosedByUser。 应用应加载其保存的应用程序数据并刷新其显示的内容。 async protected override void OnLaunched(LaunchActivatedEventArgs args) { if (args.PreviousExecutionState == ApplicationExecutionState.Terminated || args.PreviousExecutionState == ApplicationExecutionState.ClosedByUser) { // TODO: Populate the UI with the previously saved application data } else { // TODO: Populate the UI with defaults } EnsurePageCreatedAndActivate(); } Protected Overrides Sub OnLaunched(args As Windows.ApplicationModel.Activation.LaunchActivatedEventArgs) Dim restoreState As Boolean = False Select Case args.PreviousExecutionState Case ApplicationExecutionState.Terminated ' TODO: Populate the UI with the previously saved application data restoreState = True Case ApplicationExecutionState.ClosedByUser ' TODO: Populate the UI with the previously saved application data restoreState = True Case Else ' TODO: Populate the UI with defaults End Select Window.Current.Content = New MainPage(restoreState) Window.Current.Activate() End Sub void App::OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs const& e) { if (e.PreviousExecutionState() == ApplicationExecutionState::Terminated || e.PreviousExecutionState() == ApplicationExecutionState::ClosedByUser) { // Populate the UI with the previously saved application data. } else { // Populate the UI with defaults. } ... } void App::OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs^ args) { if (args->PreviousExecutionState == ApplicationExecutionState::Terminated || args->PreviousExecutionState == ApplicationExecutionState::ClosedByUser) { // TODO: Populate the UI with the previously saved application data } else { // TODO: Populate the UI with defaults } EnsurePageCreatedAndActivate(); } 如果 PreviousExecutionState 的值 NotRunning,则应用未能成功保存其应用程序数据,并且应用应重新开始,就好像它最初启动一样。 注解 注释 如果当前窗口上已设置内容,应用可以跳过初始化。 可以检查 LaunchActivatedEventArgs.TileId 属性,以确定应用是从主磁贴还是副磁贴启动,并基于该信息决定是否应提供全新的应用体验或恢复应用体验。 重要 API Windows.ApplicationModel.Activation Windows.UI.Xaml.Application 相关主题 处理应用暂停 处理应用恢复 应用暂停和恢复指南 应用生命周期