如何恢复应用(DirectX 和 C++)How to resume an app (DirectX and C++)

本主题介绍了系统在恢复通用 Windows 平台 (UWP) DirectX 应用时,如何还原重要的应用程序数据。This topic shows how to restore important application data when the system resumes your Universal Windows Platform (UWP) DirectX app.

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

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

将此代码添加到你的视图提供程序 的 IFrameworkView::Initialize 方法的实现中:Add this code to your implementation of the IFrameworkView::Initialize method of your view provider:

// The first method is called when the IFrameworkView is being created.
void App::Initialize(CoreApplicationView^ applicationView)
{
  //...
  
    CoreApplication::Resuming +=
        ref new EventHandler<Platform::Object^>(this, &App::OnResuming);
    
  //...

}

暂停之后刷新显示的内容Refresh displayed content after suspension

当你的应用处理 Resuming 事件时,它将有机会刷新其显示的内容。When your app handles the Resuming event, it has the opportunity to refresh its displayed content. 还原你已使用你的 CoreApplication::Suspending 处理程序保存的所有应用,然后重新启动处理。Restore any app you have saved with your handler for CoreApplication::Suspending, and restart processing. 游戏开发人员:如果你已暂停你的音频引擎,那么现在该重新启动它了。Game devs: if you've suspended your audio engine, now's the time to restart it.

void App::OnResuming(Platform::Object^ sender, Platform::Object^ args)
{
    // Restore any data or state that was unloaded on suspend. By default, data
    // and state are persisted when resuming from suspend. Note that this event
    // does not occur if the app was previously terminated.

    // Insert your code here.
}

对于应用的 CoreWindow,此回调是由 CoreDispatcher 处理的一条事件消息。This callback occurs as an event message processed by the CoreDispatcher for the app's CoreWindow. 如果你没有从你的应用的主回路调用 CoreDispatcher::ProcessEvents(在你的查看提供程序的 IFrameworkView::Run 方法中实现),那么将不会调用此回调。This callback will not be invoked if you do not call CoreDispatcher::ProcessEvents from your app's main loop (implemented in the IFrameworkView::Run method of your view provider).

// This method is called after the window becomes active.
void App::Run()
{
    while (!m_windowClosed)
    {
        if (m_windowVisible)
        {
            CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);

            m_main->Update();

            if (m_main->Render())
            {
                m_deviceResources->Present();
            }
        }
        else
        {
            CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending);
        }
    }
}

备注Remarks

每当用户切换到桌面或其他应用时,系统都会挂起你的应用。The system suspends your app whenever the user switches to another app or to the desktop. 每当用户切回到你的应用时,系统就会恢复你的应用。The system resumes your app whenever the user switches back to it. 当系统恢复你的应用时,你的变量和数据结构的内容与系统将你的应用暂停之前的内容相同。When the system resumes your app, the content of your variables and data structures is the same as it was before the system suspended the app. 系统会将你的应用完全恢复到你离开时的状态,使用户感觉你的应用好像一直在后台运行一样。The system restores the app exactly where it left off, so that it appears to the user as if it's been running in the background. 但是,应用可能已暂停很长一段时间,因此,它应当刷新在应用暂停之后可能已发生更改的任何显示内容,并且重新启动任何呈现或音频处理线程。However, the app may have been suspended for a significant amount of time, so it should refresh any displayed content that might have changed while the app was suspended, and restart any rendering or audio processing threads. 如果你已在上一个暂停事件期间保存任何游戏状态数据,现在请还原它。If you've saved any game state data during a previous suspend event, restore it now.