From: Jason Smith Date: Sun, 27 Mar 2016 22:48:31 +0000 (-0700) Subject: [C]Remove view from previous parent when added to new parent layout X-Git-Tag: beta-2.2.0-pre1~20^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=904d06d81269d9b03e148ab1467aeb163c8b736d;p=platform%2Fupstream%2Fxamarin-forms.git [C]Remove view from previous parent when added to new parent layout 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 --- diff --git a/Xamarin.Forms.Core.UnitTests/GroupViewUnitTests.cs b/Xamarin.Forms.Core.UnitTests/GroupViewUnitTests.cs index 61e38cf..fe823b1 100644 --- a/Xamarin.Forms.Core.UnitTests/GroupViewUnitTests.cs +++ b/Xamarin.Forms.Core.UnitTests/GroupViewUnitTests.cs @@ -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)); + } } } diff --git a/Xamarin.Forms.Core/Layout.cs b/Xamarin.Forms.Core/Layout.cs index c611777..a786407 100644 --- a/Xamarin.Forms.Core/Layout.cs +++ b/Xamarin.Forms.Core/Layout.cs @@ -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();