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

时间:2013-10-09 06:27:17

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

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

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

首先不使用调度程序,它抛出

  

Platform::AccessDeniedException

然后我提到thisthisthis。 我试图从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中获取UI的Dispatcher?

1 个答案:

答案 0 :(得分:11)

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

如果你能做这样的事情:

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