Obtaining UI dispatcher in Windows phone 8

牧云@^-^@ 提交于 2019-12-23 08:05:15

问题


I have been developing a Windows Phone application that consumes Windows Runtime component(WRC).A function accessed by a non-UI thread,needs to use a callback that access the Windows phone application.

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

At first without using the Dispatcher it threw

Platform::AccessDeniedException.

Then I referred to this, this and this. I tried to obtain the Dispatcher from the UI.

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

It threw System.AccessViolationException.Then I used

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

in C++ code(WRC).But this too throws Platform::AccessDeniedException.

How to obtain the Dispatcher for UI in Windows Phone?


回答1:


You can't get the dispatcher from C++ for Windows Phone 8: you need to move the call to the UI dispatcher on the C# side, not the C++ side.

If you can just do something like this:

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


来源:https://stackoverflow.com/questions/19264676/obtaining-ui-dispatcher-in-windows-phone-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!