处理应用恢复Handle app resume

重要的 APIImportant APIs

了解当系统恢复你的应用时刷新 UI 的情况。Learn where to refresh your UI when the system resumes your app. 本主题中的示例可向事件处理程序注册 Resuming 事件。The example in this topic registers an event handler for the Resuming event.

注册恢复事件处理程序Register the resuming event handler

注册以处理 Resuming 事件,该事件指示用户从你的应用切换到桌面或其他应用,而后又切换回你的应用。Register to handle the Resuming event, which indicates that the user switched away from your app and then back to it.

partial class MainPage
{
   public MainPage()
   {
      InitializeComponent();
      Application.Current.Resuming += new EventHandler<Object>(App_Resuming);
   }
}
Public NonInheritable Class MainPage

   Public Sub New()
      InitializeComponent()
      AddHandler Application.Current.Resuming, AddressOf App_Resuming
   End Sub

End Class
MainPage::MainPage()
{
    InitializeComponent();
    Windows::UI::Xaml::Application::Current().Resuming({ this, &MainPage::App_Resuming });
}
MainPage::MainPage()
{
    InitializeComponent();
    Application::Current->Resuming +=
        ref new EventHandler<Platform::Object^>(this, &MainPage::App_Resuming);
}

刷新显示的内容并重新获取资源Refresh displayed content and reacquire resources

在用户切换到其他应用或桌面后,系统会暂停你的应用数秒钟。The system suspends your app a few seconds after the user switches to another app or to the desktop. 每当用户切换回你的应用时,系统会恢复你的应用。The system resumes your app when the user switches back to it. 当系统恢复你的应用时,你的变量和数据结构的内容与系统将你的应用暂停之前的内容相同。When the system resumes your app, the content of your variables and data structures are the same as they were before the system suspended the app. 系统将还原离开时所使用的应用。The system restores the app where it left off. 对用户来说,就好像应用一直在后台运行一样。To the user, it appears as if the app has been running in the background.

当你的应用处理 Resuming 事件时,你的应用可能会暂停几个小时或几天。When your app handles the Resuming event, your app may be been suspended for hours or days. 它应该在应用处于暂停状态时刷新可能已过时的任何内容,例如新闻源或用户位置。It should refresh any content that might have become stale while the app was suspended, such as news feeds or the user's location.

此时,还可以还原你在应用处于暂停状态时发布的任何独占资源,例如文件句柄、相机、I/O 设备、外部设备和网络资源。This is also a good time to restore any exclusive resources that you released when your app was suspended such as file handles, cameras, I/O devices, external devices, and network resources.

partial class MainPage
{
    private void App_Resuming(Object sender, Object e)
    {
        // TODO: Refresh network data, perform UI updates, and reacquire resources like cameras, I/O devices, etc.
    }
}
Public NonInheritable Class MainPage

    Private Sub App_Resuming(sender As Object, e As Object)
 
        ' TODO: Refresh network data, perform UI updates, and reacquire resources like cameras, I/O devices, etc.

    End Sub
>
End Class
void MainPage::App_Resuming(
    Windows::Foundation::IInspectable const& /* sender */,
    Windows::Foundation::IInspectable const& /* e */)
{
    // TODO: Refresh network data, perform UI updates, and reacquire resources like cameras, I/O devices, etc.
}
void MainPage::App_Resuming(Object^ sender, Object^ e)
{
    // TODO: Refresh network data, perform UI updates, and reacquire resources like cameras, I/O devices, etc.
}

备注

由于不会从 UI 线程中引发 恢复 事件,因此必须在处理程序中使用调度程序来调度对 UI 的任何调用。Because the Resuming event is not raised from the UI thread, a dispatcher must be used in your handler to dispatch any calls to your UI.

备注Remarks

当你的应用连接到 Visual Studio 调试器时,它将不会暂停。When your app is attached to the Visual Studio debugger, it will not be suspended. 但是,你可以从调试器中暂停它,然后向其发送一个 Resume 事件,以便调试你的代码。You can suspend it from the debugger, however, and then send it a Resume event so that you can debug your code. 确保“调试位置”**** 工具栏可见并单击“暂停”**** 图标旁边的下拉列表。Make sure the Debug Location toolbar is visible and click the drop-down next to the Suspend icon. 然后选择“恢复”****。Then choose Resume.

对于 Windows Phone 应用商店应用,Resuming 事件始终后跟 OnLaunched,即使你的应用当前已暂停且用户从主要磁贴或应用列表中重新启动它也是如此。For Windows Phone Store apps, the Resuming event is always followed by OnLaunched, even when your app is currently suspended and the user re-launches your app from a primary tile or app list. 如果当前窗口上已有内容集,应用可跳过初始化。Apps can skip initialization if there is already content set on the current window. 你可以检查 LaunchActivatedEventArgs.TileId 属性以确定该应用是从主要磁贴启动还是从辅助磁贴启动,并可根据该信息,确定是应显示新的应用体验还是应恢复应用体验。You can check the LaunchActivatedEventArgs.TileId property to determine if the app was launched from a primary or a secondary tile and, based on that information, decide whether you should present a fresh or resume app experience.