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

9
我一直在开发一个消费Windows Runtime组件(WRC)的Windows Phone应用程序。由非UI线程访问的函数需要使用回调来访问Windows Phone应用程序。
void WControlPointCallback::OnListChange(char *pFriendlyName)
{
    // Callback function to access the UI
    pCallBack->AlertCaller("Message");  
}

一开始没有使用Dispatcher,它抛出了

Platform::AccessDeniedException

。然后我参考了this, thisthis。我尝试从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
请不要使用 代码标签 来强调您认为很 重要单词。代码标签是用于代码的。 - Charles
1个回答

11

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

如果你可以像这样做:

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

太好了,谢谢!这节省了很多时间。我被这个问题困扰了将近两天。 - Naren
非常感谢你,伙计! :) - cesarferreira

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接