如何繼續 app (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

當您的應用程式處理繼續事件時,就會有機會重新整理它自己的已顯示內容。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.
}

這個回呼會以 app CoreWindowCoreDispatcher 所處理之事件訊息的形式發生。This callback occurs as an event message processed by the CoreDispatcher for the app's CoreWindow. 如果您未從 app 的主迴圈 (實作於檢視提供者的 IFrameworkView::Run 方法中) 呼叫 CoreDispatcher::ProcessEvents,就不會叫用這個回呼。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. 當使用者切換回您的 app 時,系統就會繼續執行 app。The system resumes your app whenever the user switches back to it. 當系統繼續執行您的 app 時,您的變數和資料結構內容和系統暫停 app 之前一樣,沒有變化。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. 系統會將 app 回復成暫停之前的相同狀態,如此使用者會以為 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.