Windows 8 APP,用数据绑定改变XAML文本框的文本.游戏循环工作线程导致问题

Windows 8 APP, changing text of a XAML textbox with databinding... game loop worker thread causing issue

本文关键字:文本 工作 循环 游戏 线程 问题 APP 数据绑定 改变 Windows XAML      更新时间:2023-10-16

我使用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方法。