CoreWindow :: GetCurrentForThread()始终为NULL

时间:2013-01-24 08:46:25

标签: windows-runtime windows-phone-8 c++-cx

我正在尝试在Windows Phone 8上使用C ++获取CoreDispatcher,以便我可以将工作项提交到UI线程Dispatcher,以便我可以更新UI线程上的UI元素。但是,当我致电CoreWindow::GetCurrentForThread()时,我会回复NULL。在the documentation中,它声明WP8支持此功能。只要我为当前窗口获取NULL,我就无法从中获取当前Dispatcher;有谁知道如何在WP8上获得当前的Dispatcher?

2 个答案:

答案 0 :(得分:2)

CoreWindow::GetForCurrentThread()记录为返回:

  

当前活动主题的CoreWindow

如果从没有CoreWindow的线程(如任何非UI线程)调用此函数,则此函数将返回nullptr

假设应用程序已完成初始化并且有视图,您可以通过CoreApplication::MainView从应用程序的主视图中使用调度程序。或者,您可以将UI线程的Dispatcher^传递给在非UI线程上执行的代码,以便在需要调用UI线程时可以访问它。

答案 1 :(得分:1)

我用过

await CoreApplication.Views.First().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    //your code here
                });