2013-10-09 24 views
9

我一直在開發一個Windows Phone應用程序,它使用Windows運行時組件(WRC)。一個由非UI線程訪問的函數需要使用訪問Windows Phone應用程序的回調函數。在Windows Phone 8中獲取UI調度程序

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

起初不使用分派扔

Platform::AccessDeniedException

然後我稱爲thisthisthis和。 我試圖從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的分派器?

+1

請不要使用'代碼tags'強調'words',你認爲是'important'。代碼標籤用於代碼。 – Charles

+0

@Charles謝謝你的建議.. – Naren

回答

11

對於Windows Phone 8,您無法從C++獲得調度程序:您需要將調用移至C#端的UI調度程序,而不是C++端。

如果你可以做這樣的事情:

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

太棒了。謝謝,它節省了很多時間。我堅持了這個問題近兩天。 – Naren

+0

謝謝你的隊友! :) – cesarferreira

相關問題