Windows 8 APP,用数据绑定改变XAML文本框的文本.游戏循环工作线程导致问题
Windows 8 APP, changing text of a XAML textbox with databinding... game loop worker thread causing issue
我使用Direct3D使用XAML创建了一个新项目(所以我可以在最后有广告)…
我有一个textblock,并将其绑定到视图模型中的属性。
<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类。您应该将更新UI的代码放在lambda中,然后将该lambda传递给调度程序的RunAsync方法。
相关文章:
- 文本文件中的单词链表
- QSqlquery prepare()和bindvalue()不工作
- 从命令行c++发送文本文件名
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何将内容数组写入文本文件?
- C++20字符串文本模板参数工作示例
- C++程序在 freopen 中读取大文本后停止工作
- 追加到文本文件在循环中无法正常工作
- CMD窗口停止工作,文本文件使用fstream读取错误
- 无法让 sf::文本工作
- VS2013 字符文本不能作为 MSDN 的规范工作
- 我的C++从文本文件读取的程序无法正常工作,但我没有看到任何代码错误?
- 在文件中插入文本只工作一次
- 我在让非常基本的命令提示符文本处理器工作时遇到问题。ofstream() 的问题
- 移动文本模式光标不工作
- SFML文本输入不能正常工作
- c++文本文件不工作了
- 将用户输入到字符串中并将其写入文本文件-不工作
- Windows 8 APP,用数据绑定改变XAML文本框的文本.游戏循环工作线程导致问题