在Windows Phone 8中获取UI调度程序

我一直在开发一个消耗Windows运行时组件(WRC)的Windows Phone应用程序。由非UI线程访问的function需要使用访问Windows Phone应用程序的回调。

void WControlPointCallback::OnListChange(char *pFriendlyName) { // Callback function to access the UI pCallBack->AlertCaller("Message"); } 

最初没有使用Dispatcher它扔了

Platform::AccessDeniedException

然后我提到了这个 , 这个和这个 。 我试图从UI获取Dispatcher。

 var dispatcher = Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher; 

它抛出了System.AccessViolationException 。然后我用了

 pDispatcher = Windows::UI::Core::CoreWindow::GetForCurrentThread()->Dispatcher; 

在C ++代码(WRC)中。但是这也抛出了Platform::AccessDeniedException

如何在Windows Phone中获取Dispatcher for UI?

您无法从C ++ for Windows Phone 8获取调度程序:您需要将调用移动到C#端的UI调度程序,而不是C ++端。

如果你可以做这样的事情:

 class DotNetClass : IWindowsRuntimeInterface { void AlertCaller(string message) { Deployment.Current.Dispatcher.BeginInvoke(() => { MessageBox.Show(message); } } }