[C]Remove view from previous parent when added to new parent layout
authorJason Smith <jason.smith@xamarin.com>
Sun, 27 Mar 2016 22:48:31 +0000 (15:48 -0700)
committerJason Smith <jason.smith@xamarin.com>
Sun, 27 Mar 2016 22:48:31 +0000 (15:48 -0700)
Technically this could be considered a breaking change if someone was depending on the old behavior, however the old behavior resulted in layouts that were not predictable to the user. So while yes someone could have built something that works, it would have been via trial and error and generally breaking the rule of one parent to each view. Fixes https://bugzilla.xamarin.com/show_bug.cgi?id=39509

Xamarin.Forms.Core.UnitTests/GroupViewUnitTests.cs
Xamarin.Forms.Core/Layout.cs

index 61e38cf..fe823b1 100644 (file)
@@ -288,5 +288,18 @@ namespace Xamarin.Forms.Core.UnitTests
                        Assert.False (added);
                        Assert.False (removed);
                }
+
+               [Test]
+               public void AddToSecondLayoutRemovesFromOriginal()
+               {
+                       var child = new BoxView();
+                       var layout1 = new NaiveLayout();
+                       var layout2 = new NaiveLayout();
+
+                       layout1.Children.Add(child);
+                       layout2.Children.Add(child);
+
+                       Assert.False(layout1.Children.Contains(child));
+               }
        }
 }
index c611777..a786407 100644 (file)
@@ -395,6 +395,9 @@ namespace Xamarin.Forms
 
                void OnInternalAdded(View view)
                {
+                       var parent = view.Parent as Layout;
+                       parent?.InternalChildren.Remove(view);
+
                        OnChildAdded(view);
                        if (ShouldInvalidateOnChildAdded(view))
                                InvalidateLayout();