summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WinRT.Phone
diff options
context:
space:
mode:
authorE.Z. Hart <hartez@users.noreply.github.com>2017-01-26 11:51:28 -0700
committerKangho Hur <kangho.hur@samsung.com>2017-03-24 13:15:53 +0900
commit19a2511444f9355c6935beeea0bb5c974af575d0 (patch)
tree4b942f882345aa167acc6dc67c139c16b192757a /Xamarin.Forms.Platform.WinRT.Phone
parent7787b8bac9e78bd16b3519ab865a22efa564c125 (diff)
downloadxamarin-forms-19a2511444f9355c6935beeea0bb5c974af575d0.tar.gz
xamarin-forms-19a2511444f9355c6935beeea0bb5c974af575d0.tar.bz2
xamarin-forms-19a2511444f9355c6935beeea0bb5c974af575d0.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.cs108
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