From c07c2314f5737d480088319e87ab299b22cb4ddc Mon Sep 17 00:00:00 2001 From: Neil McAlister Date: Fri, 30 Sep 2016 21:47:41 +0300 Subject: [Windows] Fix Device Style inheritance (#324) * Make Device Styles get Style Setters from ancestor styles * Fix parentheses to match style --- .../WindowsPhoneResourcesProvider.cs | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'Xamarin.Forms.Platform.WinRT.Phone') diff --git a/Xamarin.Forms.Platform.WinRT.Phone/WindowsPhoneResourcesProvider.cs b/Xamarin.Forms.Platform.WinRT.Phone/WindowsPhoneResourcesProvider.cs index a4ae4e94..adbe7dea 100644 --- a/Xamarin.Forms.Platform.WinRT.Phone/WindowsPhoneResourcesProvider.cs +++ b/Xamarin.Forms.Platform.WinRT.Phone/WindowsPhoneResourcesProvider.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Linq; using Windows.UI.Text; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; @@ -25,6 +27,7 @@ namespace Xamarin.Forms.Platform.WinRT Style GetStyle (object nativeKey) { var style = (Windows.UI.Xaml.Style) Windows.UI.Xaml.Application.Current.Resources[nativeKey]; + style = GetAncestorSetters(style); var formsStyle = new Style (typeof (Label)); foreach (var b in style.Setters) { @@ -47,6 +50,48 @@ namespace Xamarin.Forms.Platform.WinRT return formsStyle; } + static Windows.UI.Xaml.Style GetAncestorSetters (Windows.UI.Xaml.Style value) + { + var style = new Windows.UI.Xaml.Style (value.TargetType) + { + BasedOn = value.BasedOn + }; + foreach (var valSetter in value.Setters) + { + style.Setters.Add(valSetter); + } + + var ancestorStyles = new List (); + + Windows.UI.Xaml.Style currStyle = style; + while (currStyle.BasedOn != null) + { + ancestorStyles.Add(currStyle.BasedOn); + currStyle = currStyle.BasedOn; + } + + foreach (var styleParent in ancestorStyles) + { + foreach (var b in styleParent.Setters) + { + var parentSetter = b as Windows.UI.Xaml.Setter; + if (parentSetter == null) + continue; + + var derviedSetter = style.Setters + .OfType () + .FirstOrDefault (x => x.Property == parentSetter.Property); + + //Ignore any ancestor setters which a child setter has already overridden + if (derviedSetter != null) + continue; + else + style.Setters.Add (parentSetter); + } + } + return style; + } + static LineBreakMode ToLineBreakMode (TextWrapping value) { switch (value) { -- cgit v1.2.3