문제

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?

도움이 되었습니까?

해결책

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);
        }
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top