Notice (2018-05-24): bugzilla.xamarin.com is now in
Please join us on
Visual Studio Developer Community and in the
Mono organizations on
GitHub to continue tracking issues. Bugzilla will remain
available for reference in read-only mode. We will continue to work
on open Bugzilla bugs, copy them to the new locations
as needed for follow-up, and add the new items under Related
Our sincere thanks to everyone who has contributed on this bug
tracker over the years. Thanks also for your understanding as we
make these adjustments and improvements for the future.
Please create a new report on
Developer Community or GitHub with
your current version information, steps to reproduce, and relevant error
messages or log files if you are hitting an issue that looks similar to
this resolved bug and you do not yet see a matching new report.
# Steps to reproduce
Create a PCL Xamarin.Forms sln. with Android and UWP/WinRT platforms. Create a ViewModel that implements INotifyPropertyChanged with a an ObservableCollection property that gets changed from a background thread. Bind a ListView's ItemsSource to the ViewModel's property.
# Expected behavior
The UI should be updated when the background thread updates the ViewModel's ObservableCollection. This *is* what happens on Android.
# Actual behavior
On Android: the ListView is updated.
" System.NullReferenceException: Object reference not set to an instance of an object.
at Xamarin.Forms.ListProxy.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
at Xamarin.Forms.ListProxy.WeakNotifyProxy.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
at System.Collections.ObjectModel.ObservableCollection`1.InsertItem(Int32 index, T item)
at System.Collections.ObjectModel.Collection`1.Add(T item)
# Supplemental info (logs, images, videos)
I was actually trying to repro a *different* bug related to ObservableCollections bound to a View, but found this one. I will continue to try to repro my original bug, as this may be related.
Using Xamarin.Forms.Device.BeginInvokeOnMainThread to modify the ObservableCollection can avoid this issue, but is not intuitive in a PCL that works on Android (iOS is untested). Is Android the outlier, or is Windows? Should be consistent across all platforms, and I believe should be handled internally to Xamarin.Forms databinding framework.
# Test environment (full version information)
Windows 10 (desktop), Visual Studio 2015 U3, Xamarin.Forms v. 1.5 or newer, occurs in both Windows 8.1 and UWP apps run on Windows 10.
To be clear, all Windows Apps were run on the *desktop* Windows OS, not on the phone OS (any version), but is assumed to be present.
This issue is going to be closed because it is not specifically a Forms issue. Per the MSDN docs, ObservableCollection<T> is not guaranteed to be thread safe, and as such an instance of it should not be modified on a background thread when bound to something in the UI. The fact that the scenario here appears to work on Android or iOS is fortunate, but not intended on our part, and could very well break at some point in the future.