问题
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