我已经使用 Direct3D 使用 XAML 创建了一个新项目(所以我最后可以有广告)...
我有一个文本 block 并将其绑定(bind)到 View 模型中的属性。
<SwapChainPanel x:Name="swapChainPanel">
<TextBlock x:Name="debugText" HorizontalAlignment="Left" Margin="204,658,0,0" TextWrapping="Wrap" Text="{Binding Debug}" VerticalAlignment="Top" />
</SwapChainPanel>
如果我在我的 XAML.cpp 文件中,并将属性值更改为某个值,则一切正常...
this->DataContext = m_deviceResources->directXPageViewModel;
m_deviceResources->directXPageViewModel->Debug = "YUMMY";
m_deviceResources->directXPageViewModel->Update( ); // runs property changed notifier
如果我在游戏循环中更改属性,屏幕会变成空白并且不会抛出明显的错误?
// So the same code as above (without this->Datacontext) placed inside my game class.
这似乎是因为游戏类渲染循环在工作线程内:
// If the animation render loop is already running then do not start another thread.
if (m_renderLoopWorker != nullptr && m_renderLoopWorker->Status == AsyncStatus::Started)
{
return;
}
// Create a task that will be run on a background thread.
//IF I PLACE THE CODE HERE, IT WORKS FINE
auto workItemHandler = ref new WorkItemHandler([this](IAsyncAction ^ action)
{
// BUT IF HERE, IT SHOWS BLACK SCREEN AND STOPS LOOPING
m_deviceResources->directXPageViewModel->Debug = "YUMMY";
m_deviceResources->directXPageViewModel->Update( );
// Calculate the updated frame and render once per vertical blanking interval.
while (action->Status == AsyncStatus::Started)
{
critical_section::scoped_lock lock(m_criticalSection);
Update(); // I'D REALLY LIKE TO PUT THE CODE IN HERE
if (Render())
{
m_deviceResources->Present( );
}
}
});
// Run task on a dedicated high priority background thread.
m_renderLoopWorker = ThreadPool::RunAsync(workItemHandler, WorkItemPriority::High, WorkItemOptions::TimeSliced);
当我需要在正确的线程中时,如何在每个循环中调用更新函数(我猜)
编辑::::::::::::::::::::::::::
我的代码现在看起来像这样
// Create a task that will be run on a background thread.
auto workItemHandler = ref new WorkItemHandler([this](IAsyncAction ^ action)
{
// Calculate the updated frame and render once per vertical blanking interval.
while (action->Status == AsyncStatus::Started)
{
critical_section::scoped_lock lock(m_criticalSection);
Update( );
// THIS IS THE NEW CALL BUT IT CRASHES MY GAME :)
CoreWindow::GetForCurrentThread( )->Dispatcher->RunAsync( CoreDispatcherPriority::Normal, ref new Windows::UI::Core::DispatchedHandler( [ this ]( )
{
m_deviceResources->directXPageViewModel->Debug = "YUMMY";
m_deviceResources->directXPageViewModel->Update( );
} ) );
if (Render())
{
m_deviceResources->Present( );
}
}
});
这有一些问题,很难找出到底发生了什么。我认为 CoreDispatcher 是 NULL 或其他东西,所以它崩溃了:
Unhandled exception at 0x00BB84C7 in Game.exe: 0xC0000005: Access violation reading location 0x00000000.
directXPageViewModel.h
最佳答案
看看 CoreDispatcher类(class)。您应该将更新 UI 的代码放在 lambda 中,然后将该 lambda 传递给调度程序的 RunAsync 方法。
关于c++ - Windows 8 APP,使用数据绑定(bind)更改 XAML 文本框的文本...导致问题的游戏循环工作线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22520626/