本主题演示如何定义通用 Windows 平台 (UWP) DirectX 应用的激活体验。
注册应用激活事件处理程序
首先,注册以处理 CoreApplicationView::Activated 事件,该事件是在作系统启动和初始化应用时引发的。
将此代码添加到视图提供程序的 IFrameworkView::Initialize 方法的实现(在示例中命名为 MyViewProvider):
void App::Initialize(CoreApplicationView^ applicationView)
{
// Register event handlers for the app lifecycle. This example includes Activated, so that we
// can make the CoreWindow active and start rendering on the window.
applicationView->Activated +=
ref new TypedEventHandler<CoreApplicationView^, IActivatedEventArgs^>(this, &App::OnActivated);
//...
}
激活应用的 CoreWindow 实例
应用启动时,必须获取对应用的 CoreWindow 的引用。 CoreWindow 包含应用用于处理窗口事件的窗口事件消息调度程序。 通过调用 CoreWindow::GetForCurrentThread,在应用激活事件的回调中获取此引用。 获取此引用后,通过调用 CoreWindow::Activate 激活主应用窗口。
void App::OnActivated(CoreApplicationView^ applicationView, IActivatedEventArgs^ args)
{
// Run() won't start until the CoreWindow is activated.
CoreWindow::GetForCurrentThread()->Activate();
}
开始处理主应用窗口的事件消息
回调在应用的 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);
}
}
}
相关主题