From 51654555d8776ffc14abd5c91a396fabac51c375 Mon Sep 17 00:00:00 2001 From: Andrei Nitescu Date: Wed, 10 Jul 2019 05:03:16 +0300 Subject: [PATCH] Fix Page implicit and inherited style with ControlTemplate #6657 (#6661) * Fix Page implicit and inherited style with ControlTemplate #6657 * Update StyleTests.cs fixes #6657 --- Xamarin.Forms.Core.UnitTests/StyleTests.cs | 71 +++++++++++++++++++++++++++++- Xamarin.Forms.Core/Page.cs | 5 +++ 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/Xamarin.Forms.Core.UnitTests/StyleTests.cs b/Xamarin.Forms.Core.UnitTests/StyleTests.cs index a46883d..0d6bf94 100644 --- a/Xamarin.Forms.Core.UnitTests/StyleTests.cs +++ b/Xamarin.Forms.Core.UnitTests/StyleTests.cs @@ -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 +} diff --git a/Xamarin.Forms.Core/Page.cs b/Xamarin.Forms.Core/Page.cs index 7ee9981..ac0b784 100644 --- a/Xamarin.Forms.Core/Page.cs +++ b/Xamarin.Forms.Core/Page.cs @@ -58,6 +58,11 @@ namespace Xamarin.Forms var toolbarItems = new ObservableCollection(); 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>(() => new PlatformConfigurationRegistry(this)); } -- 2.7.4