Fix Page implicit and inherited style with ControlTemplate #6657 (#6661)
authorAndrei Nitescu <nitescua@yahoo.com>
Wed, 10 Jul 2019 02:03:16 +0000 (05:03 +0300)
committerSamantha Houts <samhouts@users.noreply.github.com>
Wed, 10 Jul 2019 02:03:16 +0000 (19:03 -0700)
* Fix Page implicit and inherited style with ControlTemplate #6657

* Update StyleTests.cs
fixes #6657

Xamarin.Forms.Core.UnitTests/StyleTests.cs
Xamarin.Forms.Core/Page.cs

index a46883d..0d6bf94 100644 (file)
@@ -759,6 +759,73 @@ namespace Xamarin.Forms.Core.UnitTests
                        Assert.That(label0.TextColor, Is.EqualTo(Color.Pink));
                        Assert.That(label1.TextColor, Is.EqualTo(Color.Lavender));
                }
+               
+               [Test]
+               public void ImplicitInheritedStyleForTemplatedElementIsAppliedCorrectlyForContentPage()
+               {
+                       var controlTemplate = new ControlTemplate(typeof(ContentPresenter));
+
+                       var rd0 = new ResourceDictionary {
+                               new Style (typeof(ContentPage)) {
+                                       Setters = {
+                                               new Setter {Property = TemplatedPage.ControlTemplateProperty, Value = controlTemplate}
+                                       },
+                                       ApplyToDerivedTypes = true
+                               }
+                       };
+
+                       var mockApp = new MockApplication();
+                       mockApp.Resources = rd0;
+                       mockApp.MainPage = new MyPage()
+                       {
+                               Content = new Button()
+                       };
+
+                       Application.Current = mockApp;
+
+                       var parentPage = (ContentPage)mockApp.MainPage;
+                       var pageContent = parentPage.Content;
+                       Assert.That(Equals(pageContent?.Parent, parentPage));
+               }
+
+               [Test]
+               public void ImplicitInheritedStyleForTemplatedElementIsAppliedCorrectlyForContentView()
+               {
+                       var controlTemplate = new ControlTemplate(typeof(ContentPresenter));
+
+                       var rd0 = new ResourceDictionary {
+                               new Style (typeof(ContentView)) {
+                                       Setters = {
+                                               new Setter {Property = TemplatedView.ControlTemplateProperty, Value = controlTemplate}
+                                       },
+                                       ApplyToDerivedTypes = true
+                               }
+                       };
+
+                       var mockApp = new MockApplication();
+                       mockApp.Resources = rd0;
+                       mockApp.MainPage = new ContentPage()
+                       {
+                               Content = new MyContentView()
+                               {
+                                       Content = new Button()
+                               }
+                       };
+
+                       Application.Current = mockApp;
+
+                       var parentView = (ContentView)((ContentPage)mockApp.MainPage).Content;
+                       var content = parentView.Content;
+                       Assert.That(Equals(content?.Parent, parentView));
+               }
+       
+               class MyPage : ContentPage
+               {
+               }
+
+               class MyContentView : ContentView
+               {
+               }
 
                [Test]
                public void MismatchTargetTypeThrowsError1()
@@ -782,6 +849,6 @@ namespace Xamarin.Forms.Core.UnitTests
                        var s = new Style(typeof(View));
                        var t = new Button();
                        Assert.DoesNotThrow(() => t.Style = s);
-               }
+       }
        }
-}
\ No newline at end of file
+}
index 7ee9981..ac0b784 100644 (file)
@@ -58,6 +58,11 @@ namespace Xamarin.Forms
                        var toolbarItems = new ObservableCollection<ToolbarItem>();
                        toolbarItems.CollectionChanged += OnToolbarItemsCollectionChanged;
                        ToolbarItems = toolbarItems;
+
+                       //if things were added in base ctor (through implicit styles), the items added aren't properly parented
+                       if (InternalChildren.Count > 0)
+                               InternalChildrenOnCollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, InternalChildren));
+
                        InternalChildren.CollectionChanged += InternalChildrenOnCollectionChanged;
                        _platformConfigurationRegistry = new Lazy<PlatformConfigurationRegistry<Page>>(() => new PlatformConfigurationRegistry<Page>(this));
                }