如何恢复应用(DirectX 和 C++)

本主题介绍了系统在恢复通用 Windows 平台 (UWP) DirectX 应用时,如何还原重要的应用程序数据。

注册恢复事件处理程序

注册以处理 CoreApplication::Resuming 事件, 该事件指示用户从你的应用切换离开,而后又切换回你的应用。

将此代码添加到你的视图提供程序 的 IFrameworkView::Initialize 方法的实现中:

// 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);
    
  //...

}

暂停之后刷新显示的内容

当你的应用处理 Resuming 事件时,它将有机会刷新其显示的内容。 还原你已使用你的 CoreApplication::Suspending 处理程序保存的所有应用,然后重新启动处理。 游戏开发人员:如果你已暂停你的音频引擎,那么现在该重新启动它了。

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 处理的一条事件消息。 如果你没有从你的应用的主回路调用 CoreDispatcher::ProcessEvents(在你的查看提供程序的 IFrameworkView::Run 方法中实现),那么将不会调用此回调。

// 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);
        }
    }
}

注解

每当用户切换到桌面或其他应用时,系统都会挂起你的应用。 每当用户切回到你的应用时,系统就会恢复你的应用。 当系统恢复你的应用时,你的变量和数据结构的内容与系统将你的应用暂停之前的内容相同。 系统会将你的应用完全恢复到你离开时的状态,使用户感觉你的应用好像一直在后台运行一样。 但是,应用可能已暂停很长一段时间,因此,它应当刷新在应用暂停之后可能已发生更改的任何显示内容,并且重新启动任何呈现或音频处理线程。 如果你已在上一个暂停事件期间保存任何游戏状态数据,现在请还原它。