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

Nar*_*ren 9 c# windows-runtime c++-cx windows-phone-8

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

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

最初没有使用Dispatcher它扔了

Platform::AccessDeniedException.

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

var dispatcher = Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher;
Run Code Online (Sandbox Code Playgroud)

它扔了System.AccessViolationException.然后我用了

pDispatcher = Windows::UI::Core::CoreWindow::GetForCurrentThread()->Dispatcher; 
Run Code Online (Sandbox Code Playgroud)

在C++代码(WRC)中.但这也引发了Platform::AccessDeniedException.

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

Pau*_*tts 11

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

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

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