所以,首先我已经阅读了大量关于这个特定问题的线程,但我仍然不明白如何解决它。基本上,我正在尝试与 websocket 通信并将收到的消息存储在绑定到列表视图的可观察集合中。我知道我从套接字正确地得到了响应,但是当它尝试将它添加到可观察集合中时,它给了我以下错误:
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;
}
如何在仍然绑定到我的列表视图的可观察集合的同时使错误消失?