该应用程序称为一个接口,编组为一个不同的线程 – Windowsapp store应用程序

所以,首先我已经阅读了关于这个特定问题的大量的线索,我仍然不知道如何解决这个问题。 基本上,我正在尝试与websocket进行通信,并将收到的消息存储在绑定到listview的可观察集合中。 我知道我正在从套接字得到一个响应正确,但是当它试图将其添加到可观察集合,它给了我以下错误:

The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD)) 

我已经阅读了一些关于“派遣”的信息以及其他的一些信息,但是我很困惑! 这是我的代码:

 public ObservableCollection<string> messageList { get; set; } private void MessageReceived(MessageWebSocket sender, MessageWebSocketMessageReceivedEventArgs args) { string read = ""; try { using (DataReader reader = args.GetDataReader()) { reader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8; read = reader.ReadString(reader.UnconsumedBufferLength); } } catch (Exception ex) // For debugging { WebErrorStatus status = WebSocketError.GetStatus(ex.GetBaseException().HResult); // Add your specific error-handling code here. } if (read != "") messageList.Add(read); // this is where I get the error } 

这是绑定:

 protected override async void OnNavigatedTo(NavigationEventArgs e) { //await Authenticate(); Gameboard.DataContext = Game.GameDetails.Singleton; lstHighScores.ItemsSource = sendInfo.messageList; } 

如何使错误消失,同时绑定到我的listview的可观察集合?

这解决了我的问题:

 Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { // Your UI update code goes here! } ); 

在Windowsapp store应用中获取CoreDispatcher的正确方法

尝试更换

 messageList.Add(read); 

 Dispatcher.Invoke((Action)(() => messageList.Add(read))); 

如果您从Window类以外的地方打电话,请尝试:

 Application.Current.Dispatcher.Invoke((Action)(() => messageList.Add(read)));