Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Fix Page implicit and inherited style with ControlTemplate #6657 (#6661)
Browse files Browse the repository at this point in the history
* Fix Page implicit and inherited style with ControlTemplate #6657

* Update StyleTests.cs
fixes #6657
  • Loading branch information
andreinitescu authored and samhouts committed Jul 10, 2019
1 parent feb0072 commit 5165455
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
71 changes: 69 additions & 2 deletions Xamarin.Forms.Core.UnitTests/StyleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,73 @@ public void ReplacingResourcesDoesNotOverrideManuallySetProperties()
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()
Expand All @@ -782,6 +849,6 @@ public void MatchTargetTypeDoesntThrowError()
var s = new Style(typeof(View));
var t = new Button();
Assert.DoesNotThrow(() => t.Style = s);
}
}
}
}
}
5 changes: 5 additions & 0 deletions Xamarin.Forms.Core/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public Page()
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));
}
Expand Down

0 comments on commit 5165455

Please sign in to comment.