Obtaining UI dispatcher in Windows phone 8
Asked Answered
A

1

9

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?

Apomixis answered 9/10, 2013 at 6:27 Comment(1)
Please don't use code tags to emphasize words that you think are important. Code tags are for code.Thibodeaux
H
11

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);
        }
    }
}
Hau answered 9/10, 2013 at 16:45 Comment(1)
That's great.Thanks,It saved a lot of time.I stuck up with this issue for nearly two days.Apomixis

© 2022 - 2024 — McMap. All rights reserved.