When trying to add data to an ObservableCollection, which is x:Binded in XAML to a ListView on the UI, I get this error:The application called an interface that was marshalled for a different thread (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))
My app implements MVVM. VieModel pulls data with an eventhandler. I have tried various SO and other solutions, such as:
var ignored = Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,() =>ViewList.Add(msg));
This gives the error: Current.Get == null
Can't access Dispatcher directly, as it is being called in the ViewModel. Also no .Invoke or .BeginInvoke access that I could find which several solutions suggested.
I tried using DispatcherTimer, as accoring to: Updating ObservableCollection from non UI thread. I get the "WRONG_THREAD"
error message when trying to instantiate DispatcherTimer in the ViewModel, to access the UI thread:
disPatchTimer = new DispatcherTimer();
There is one suggestion that works, which many have upvoted here: The application called an interface that was marshalled for a different thread - Windows Store App
Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
// Your UI update code goes here!
}
);
Which does not, at least, look like an elegant solution the "creators" had in mind to properly use code?