Update BindableLayout.cs to fix #5213 (#5243)
authorJeremy Marcus <jsmarcus@users.noreply.github.com>
Mon, 25 Feb 2019 19:44:00 +0000 (14:44 -0500)
committerStephane Delcroix <stephane@delcroix.org>
Mon, 25 Feb 2019 19:44:00 +0000 (20:44 +0100)
- fixes #5213

* Add Test for BindableLayout RemoveAll

* Update BindableLayout.cs to fix RemoveAll exception

* Missing using statements

Xamarin.Forms.Core.UnitTests/BindableLayoutTests.cs
Xamarin.Forms.Core/BindableLayout.cs

index 60532c9..e2a6ddc 100644 (file)
@@ -1,8 +1,10 @@
 using NUnit.Framework;
 using System;
 using System.Collections;
+using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.Collections.Specialized;
+using System.ComponentModel;
 using System.Linq;
 
 namespace Xamarin.Forms.Core.UnitTests
@@ -73,6 +75,21 @@ namespace Xamarin.Forms.Core.UnitTests
                }
 
                [Test]
+               public void TracksRemoveAll()
+               {
+                       var layout = new StackLayout
+                       {
+                               IsPlatformEnabled = true,
+                       };
+
+                       var itemsSource = new ObservableRangeCollection<int>(Enumerable.Range(0, 10));
+                       BindableLayout.SetItemsSource(layout, itemsSource);
+
+                       itemsSource.RemoveAll();
+                       Assert.IsTrue(IsLayoutWithItemsSource(itemsSource, layout));
+               }
+
+               [Test]
                public void TracksReplace()
                {
                        var layout = new StackLayout
@@ -377,6 +394,27 @@ namespace Xamarin.Forms.Core.UnitTests
                        }
                }
 
+               class ObservableRangeCollection<T> : ObservableCollection<T>
+               {
+                       public ObservableRangeCollection(IEnumerable<T> collection)
+                               : base(collection)
+                       {
+                       }
+
+                       public void RemoveAll()
+                       {
+                               CheckReentrancy();
+
+                               var changedItems = new List<T>(Items);
+                               foreach (var i in changedItems)
+                                       Items.Remove(i);
+
+                               OnPropertyChanged(new PropertyChangedEventArgs("Count"));
+                               OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
+                               OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, changedItems, 0));
+                       }
+               }     
+      
                class MyDataTemplateSelectorTest : DataTemplateSelector
                {
                        readonly Func<object, BindableObject, DataTemplate> _func;
index 271baf2..228071e 100644 (file)
@@ -223,9 +223,9 @@ namespace Xamarin.Forms
                                        {
                                                if (e.OldStartingIndex == -1)
                                                        goto case NotifyCollectionChangedAction.Reset;
-                                               for (int i = 0; i < e.OldItems.Count; ++i)
+                                               for (int i = 0; i < e.OldItems.Count; i++)
                                                {
-                                                       layout.Children.RemoveAt(i + e.OldStartingIndex);
+                                                       layout.Children.RemoveAt(e.OldStartingIndex);
                                                }
                                        }
                                        break;