From 520ff4a227d599a2ef68ed13478cff1bd3308bb3 Mon Sep 17 00:00:00 2001 From: "E.Z. Hart" Date: Thu, 26 Jan 2017 11:51:28 -0700 Subject: 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 --- .../WindowsResourcesProvider.cs | 67 +++++++++------------- 1 file changed, 27 insertions(+), 40 deletions(-) (limited to 'Xamarin.Forms.Platform.WinRT.Tablet') diff --git a/Xamarin.Forms.Platform.WinRT.Tablet/WindowsResourcesProvider.cs b/Xamarin.Forms.Platform.WinRT.Tablet/WindowsResourcesProvider.cs index 64041c00..c4485679 100644 --- a/Xamarin.Forms.Platform.WinRT.Tablet/WindowsResourcesProvider.cs +++ b/Xamarin.Forms.Platform.WinRT.Tablet/WindowsResourcesProvider.cs @@ -1,7 +1,7 @@ -using System; -using Windows.UI.Text; +using Windows.UI.Text; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; +using WStyle = Windows.UI.Xaml.Style; #if WINDOWS_UWP @@ -15,60 +15,47 @@ namespace Xamarin.Forms.Platform.WinRT { public IResourceDictionary GetSystemResources() { - Windows.UI.Xaml.ResourceDictionary windowsResources = Windows.UI.Xaml.Application.Current.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("CaptionTextBlockStyle", prototype), + [Device.Styles.ListItemDetailTextStyleKey] = GetStyle("BodyTextBlockStyle", prototype), - 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("CaptionTextBlockStyle"); #if WINDOWS_UWP - resources[Device.Styles.ListItemTextStyleKey] = GetStyle("BaseTextBlockStyle"); + [Device.Styles.ListItemTextStyleKey] = GetStyle("BaseTextBlockStyle", prototype), #else - resources[Device.Styles.ListItemTextStyleKey] = GetStyle("TitleTextBlockStyle"); + [Device.Styles.ListItemTextStyleKey] = GetStyle("TitleTextBlockStyle", prototype), #endif - resources[Device.Styles.ListItemDetailTextStyleKey] = GetStyle("BodyTextBlockStyle"); - return resources; + }; } - Style GetStyle(object nativeKey) + Style GetStyle(object nativeKey, TextBlock prototype) { - var style = (Windows.UI.Xaml.Style)Windows.UI.Xaml.Application.Current.Resources[nativeKey]; + var style = (WStyle)Windows.UI.Xaml.Application.Current.Resources[nativeKey]; - var formsStyle = new Style(typeof(Label)); - foreach (SetterBase 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)); - try - { - 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)); - } - catch (NotImplementedException) - { - // see https://bugzilla.xamarin.com/show_bug.cgi?id=33135 - // WinRT implementation of Windows.UI.Xaml.Setter.get_Value is not implemented. - } - } + 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 FontAttributes ToAttributes(ushort uweight) + static FontAttributes ToAttributes(FontWeight fontWeight) { - if (uweight == FontWeights.Bold.Weight || uweight == FontWeights.SemiBold.Weight || uweight == FontWeights.ExtraBold.Weight) + if (fontWeight.Weight == FontWeights.Bold.Weight || fontWeight.Weight == FontWeights.SemiBold.Weight + || fontWeight.Weight == FontWeights.ExtraBold.Weight) + { return FontAttributes.Bold; + } return FontAttributes.None; } -- cgit v1.2.3