There appears to be a bug in the ListView control for Windows Store apps. If you try to change the ItemsSource after it is loaded it throws an exception. The same exception is thrown if I initially assign an ObservableCollection and use its Clear() function.
public class App : Application
{
public App()
{
MainPage = new MyPage();
}
}
public class MyPage: ContentPage
{
public MyPage()
{
var items = new[] { "Fred", "Barney", "Wilma", "Betty" };
var listView = new ListView { ItemsSource = items };
var testButton = new Button { Text = "Test" };
// clicking this button will cause the exception
testButton.Clicked += (object sender, EventArgs e) => listView.ItemsSource = null;
Content = new StackLayout {
Orientation = StackOrientation.Vertical,
Children =
{
testButton,
listView
}
};
}
}
In the above sample if clicking the testButton will throw the following exception:
This collection cannot work with indices larger than Int32.MaxValue - 1 (0x7FFFFFFF - 1). Parameter name: index
at System.Runtime.InteropServices.WindowsRuntime.ListToBindableVectorAdapter.EnsureIndexInt32(UInt32 index, Int32 listCapacity)
at System.Runtime.InteropServices.WindowsRuntime.ListToBindableVectorAdapter.GetAt(UInt32 index)
at Xamarin.Forms.TemplatedItemsList`2.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
at Xamarin.Forms.TemplatedItemsList`2.OnProxyCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e, Boolean fixWindows)
at Xamarin.Forms.TemplatedItemsList`2.OnItemsSourceChanged(Boolean fromGrouping)
at Xamarin.Forms.TemplatedItemsList`2.BindableOnPropertyChanged(Object sender, PropertyChangedEventArgs e)
at System.ComponentModel.PropertyChangedEventHandler.Invoke(Object sender, PropertyChangedEventArgs e)
at Xamarin.Forms.BindableObject.OnPropertyChanged(String propertyName)
at Xamarin.Forms.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, Boolean silent)
at Xamarin.Forms.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes)
at Xamarin.Forms.BindableObject.SetValue(BindableProperty property, Object value, Boolean fromStyle, Boolean checkAccess)
at Xamarin.Forms.ItemsView`1.set_ItemsSource(IEnumerable value)
at ClassLibrary1.MyPage.<>c__DisplayClass0_0.<.ctor>b__0(Object sender, EventArgs e)
at Xamarin.Forms.Button.Xamarin.Forms.IButtonController.SendClicked()
at Xamarin.Forms.Platform.WinRT.ButtonRenderer.OnButtonClick(Object sender, RoutedEventArgs e)