diff options
author | E.Z. Hart <hartez@users.noreply.github.com> | 2017-01-26 11:51:28 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-26 11:51:28 -0700 |
commit | 520ff4a227d599a2ef68ed13478cff1bd3308bb3 (patch) | |
tree | c4a2ce95ad7404bccee0b08c35d7bd27ed9408f1 /Xamarin.Forms.Platform.WinRT.Phone | |
parent | c013452734ea5c8745f3ee849a40e0c65332fb82 (diff) | |
download | xamarin-forms-520ff4a227d599a2ef68ed13478cff1bd3308bb3.tar.gz xamarin-forms-520ff4a227d599a2ef68ed13478cff1bd3308bb3.tar.bz2 xamarin-forms-520ff4a227d599a2ef68ed13478cff1bd3308bb3.zip |
Use a prototype TextBlock to resolve theme resources on Windows (#703)
* Use a prototype TextBlock to resolve theme resources on Windows
* Move textblock declaration to beginning of class
* Don't retain prototype TextBlock in memory
Diffstat (limited to 'Xamarin.Forms.Platform.WinRT.Phone')
-rw-r--r-- | Xamarin.Forms.Platform.WinRT.Phone/WindowsPhoneResourcesProvider.cs | 108 |
1 files changed, 29 insertions, 79 deletions
diff --git a/Xamarin.Forms.Platform.WinRT.Phone/WindowsPhoneResourcesProvider.cs b/Xamarin.Forms.Platform.WinRT.Phone/WindowsPhoneResourcesProvider.cs index adbe7dea..7b05159c 100644 --- a/Xamarin.Forms.Platform.WinRT.Phone/WindowsPhoneResourcesProvider.cs +++ b/Xamarin.Forms.Platform.WinRT.Phone/WindowsPhoneResourcesProvider.cs @@ -1,9 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using Windows.UI.Text; +using Windows.UI.Text; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; +using WStyle = Windows.UI.Xaml.Style; namespace Xamarin.Forms.Platform.WinRT { @@ -12,89 +10,50 @@ namespace Xamarin.Forms.Platform.WinRT { public IResourceDictionary GetSystemResources() { - var windowsResources = Windows.UI.Xaml.Application.Current.Resources; - - var resources = new ResourceDictionary (); - resources[Device.Styles.TitleStyleKey] = GetStyle ("HeaderTextBlockStyle"); - resources[Device.Styles.SubtitleStyleKey] = GetStyle ("SubheaderTextBlockStyle"); - resources[Device.Styles.BodyStyleKey] = GetStyle ("BodyTextBlockStyle"); - resources[Device.Styles.CaptionStyleKey] = GetStyle ("BodyTextBlockStyle"); - resources[Device.Styles.ListItemTextStyleKey] = GetStyle ("ListViewItemTextBlockStyle"); - resources[Device.Styles.ListItemDetailTextStyleKey] = GetStyle ("ListViewItemContentTextBlockStyle"); - return resources; + var prototype = new TextBlock(); + + return new ResourceDictionary + { + [Device.Styles.TitleStyleKey] = GetStyle("HeaderTextBlockStyle", prototype), + [Device.Styles.SubtitleStyleKey] = GetStyle("SubheaderTextBlockStyle", prototype), + [Device.Styles.BodyStyleKey] = GetStyle("BodyTextBlockStyle", prototype), + [Device.Styles.CaptionStyleKey] = GetStyle("BodyTextBlockStyle", prototype), + [Device.Styles.ListItemTextStyleKey] = GetStyle("ListViewItemTextBlockStyle", prototype), + [Device.Styles.ListItemDetailTextStyleKey] = GetStyle("ListViewItemContentTextBlockStyle", prototype) + }; } - Style GetStyle (object nativeKey) + static Style GetStyle(object nativeKey, TextBlock prototype) { - var style = (Windows.UI.Xaml.Style) Windows.UI.Xaml.Application.Current.Resources[nativeKey]; - style = GetAncestorSetters(style); + var style = (WStyle)Windows.UI.Xaml.Application.Current.Resources[nativeKey]; - var formsStyle = new Style (typeof (Label)); - foreach (var b in style.Setters) { - var setter = b as Windows.UI.Xaml.Setter; - if (setter == null) - continue; + prototype.Style = style; - // TODO: Need to implement a stealth pass-through for things we don't support + var formsStyle = new Style(typeof(Label)); - if (setter.Property == TextBlock.FontSizeProperty) - formsStyle.Setters.Add (Label.FontSizeProperty, setter.Value); - else if (setter.Property == TextBlock.FontFamilyProperty) - formsStyle.Setters.Add (Label.FontFamilyProperty, setter.Value); - else if (setter.Property == TextBlock.FontWeightProperty) - formsStyle.Setters.Add (Label.FontAttributesProperty, ToAttributes (Convert.ToUInt16 (setter.Value))); - else if (setter.Property == TextBlock.TextWrappingProperty) - formsStyle.Setters.Add (Label.LineBreakModeProperty, ToLineBreakMode ((TextWrapping) setter.Value)); - } + formsStyle.Setters.Add(Label.FontSizeProperty, prototype.FontSize); + formsStyle.Setters.Add(Label.FontFamilyProperty, prototype.FontFamily.Source); + formsStyle.Setters.Add(Label.FontAttributesProperty, ToAttributes(prototype.FontWeight)); + formsStyle.Setters.Add(Label.LineBreakModeProperty, ToLineBreakMode(prototype.TextWrapping)); return formsStyle; } - static Windows.UI.Xaml.Style GetAncestorSetters (Windows.UI.Xaml.Style value) + static FontAttributes ToAttributes(FontWeight fontWeight) { - var style = new Windows.UI.Xaml.Style (value.TargetType) - { - BasedOn = value.BasedOn - }; - foreach (var valSetter in value.Setters) + if (fontWeight.Weight == FontWeights.Bold.Weight || fontWeight.Weight == FontWeights.SemiBold.Weight + || fontWeight.Weight == FontWeights.ExtraBold.Weight) { - style.Setters.Add(valSetter); - } - - var ancestorStyles = new List<Windows.UI.Xaml.Style> (); - - Windows.UI.Xaml.Style currStyle = style; - while (currStyle.BasedOn != null) - { - ancestorStyles.Add(currStyle.BasedOn); - currStyle = currStyle.BasedOn; + return FontAttributes.Bold; } - 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<Windows.UI.Xaml.Setter> () - .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; + return FontAttributes.None; } - static LineBreakMode ToLineBreakMode (TextWrapping value) + static LineBreakMode ToLineBreakMode(TextWrapping value) { - switch (value) { + switch (value) + { case TextWrapping.Wrap: return LineBreakMode.CharacterWrap; case TextWrapping.WrapWholeWords: @@ -104,14 +63,5 @@ namespace Xamarin.Forms.Platform.WinRT return LineBreakMode.NoWrap; } } - - static FontAttributes ToAttributes (ushort uweight) - { - if (uweight == FontWeights.Bold.Weight || uweight == FontWeights.SemiBold.Weight || uweight == FontWeights.ExtraBold.Weight) { - return FontAttributes.Bold; - } - - return FontAttributes.None; - } } }
\ No newline at end of file |