From 30c0dcb949186c21c60c4c9ddf8a581d40a43662 Mon Sep 17 00:00:00 2001 From: "E.Z. Hart" Date: Tue, 16 Aug 2016 12:10:32 -0600 Subject: Fix Entry/SearchBar color issues (#306) * Fix disappearing Entry text on UWP Anniversary Edition Fix background color reversion bug in UWP Phone Move SearchBar styling on UWP to its own file Make foreground/background color changes on UWP SearchBar/Entry consistent Fix SearchBar color toggle bug on WP8 * Temporarily moving SDK target to previous version * Fix build error on OSX --- Xamarin.Forms.Platform.WP8/BrushHelpers.cs | 37 ++++++++++++++++++ Xamarin.Forms.Platform.WP8/EntryRenderer.cs | 45 +++++----------------- Xamarin.Forms.Platform.WP8/SearchBarRenderer.cs | 36 ++++------------- .../Xamarin.Forms.Platform.WP8.csproj | 1 + 4 files changed, 55 insertions(+), 64 deletions(-) create mode 100644 Xamarin.Forms.Platform.WP8/BrushHelpers.cs (limited to 'Xamarin.Forms.Platform.WP8') diff --git a/Xamarin.Forms.Platform.WP8/BrushHelpers.cs b/Xamarin.Forms.Platform.WP8/BrushHelpers.cs new file mode 100644 index 00000000..993b23f4 --- /dev/null +++ b/Xamarin.Forms.Platform.WP8/BrushHelpers.cs @@ -0,0 +1,37 @@ +using System; +using System.Windows.Media; + +namespace Xamarin.Forms.Platform.WinPhone +{ + internal static class BrushHelpers + { + /// + /// Handles the logic for setting a Xamarin.Forms Color for a Brush + /// while caching the original default brush + /// + /// The target Xamarin.Forms.Color + /// The renderer's cache for the default brush + /// Delegate for retrieving the Control's current Brush + /// Delegate for setting the Control's Brush + public static void UpdateColor(Color color, ref Brush defaultbrush, Func getter, Action setter) + { + if (color.IsDefault) + { + if (defaultbrush == null) + { + return; + } + + setter(defaultbrush); + return; + } + + if (defaultbrush == null) + { + defaultbrush = getter(); + } + + setter(color.ToBrush()); + } + } +} diff --git a/Xamarin.Forms.Platform.WP8/EntryRenderer.cs b/Xamarin.Forms.Platform.WP8/EntryRenderer.cs index b2749c05..00fdbb4a 100644 --- a/Xamarin.Forms.Platform.WP8/EntryRenderer.cs +++ b/Xamarin.Forms.Platform.WP8/EntryRenderer.cs @@ -77,6 +77,7 @@ namespace Xamarin.Forms.Platform.WinPhone bool _fontApplied; bool _ignoreTextChange; Brush _placeholderDefaultBrush; + Brush _textDefaultBrush; public override SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint) { @@ -216,23 +217,12 @@ namespace Xamarin.Forms.Platform.WinPhone if (Control == null) return; - Entry entry = Element; - if (entry != null) - { - if (!IsNullOrEmpty(entry.Text)) - { - if (!entry.TextColor.IsDefault) - Control.Foreground = entry.TextColor.ToBrush(); - else - Control.Foreground = (Brush)WControl.ForegroundProperty.GetMetadata(typeof(FormsPhoneTextBox)).DefaultValue; - - // Force the PhoneTextBox control to do some internal bookkeeping - // so the colors change immediately and remain changed when the control gets focus - Control.OnApplyTemplate(); - } - } - else - Control.Foreground = (Brush)WControl.ForegroundProperty.GetMetadata(typeof(FormsPhoneTextBox)).DefaultValue; + BrushHelpers.UpdateColor(Element.TextColor, ref _textDefaultBrush, + () => Control.Foreground, brush => Control.Foreground = brush); + + // Force the PhoneTextBox control to do some internal bookkeeping + // so the colors change immediately and remain changed when the control gets focus + Control.OnApplyTemplate(); } void UpdateFont() @@ -286,25 +276,8 @@ namespace Xamarin.Forms.Platform.WinPhone void UpdatePlaceholderColor() { - Color placeholderColor = Element.PlaceholderColor; - - if (placeholderColor.IsDefault) - { - if (_placeholderDefaultBrush == null) - return; - - // Use the cached default brush - Control.PlaceholderForegroundBrush = _placeholderDefaultBrush; - return; - } - - if (_placeholderDefaultBrush == null) - { - // Cache the default brush in case we need to set the color back to default - _placeholderDefaultBrush = Control.PlaceholderForegroundBrush; - } - - Control.PlaceholderForegroundBrush = placeholderColor.ToBrush(); + BrushHelpers.UpdateColor(Element.PlaceholderColor, ref _placeholderDefaultBrush, + () => Control.PlaceholderForegroundBrush, brush => Control.PlaceholderForegroundBrush = brush); } void UpdateText() diff --git a/Xamarin.Forms.Platform.WP8/SearchBarRenderer.cs b/Xamarin.Forms.Platform.WP8/SearchBarRenderer.cs index a71041da..b37978d0 100644 --- a/Xamarin.Forms.Platform.WP8/SearchBarRenderer.cs +++ b/Xamarin.Forms.Platform.WP8/SearchBarRenderer.cs @@ -117,20 +117,8 @@ namespace Xamarin.Forms.Platform.WinPhone void UpdatePlaceholderColor() { - Color placeholderColor = Element.PlaceholderColor; - - if (placeholderColor.IsDefault) - { - if (_defaultPlaceholderColorBrush == null) - return; - - Control.PlaceholderForegroundBrush = _defaultPlaceholderColorBrush; - } - - if (_defaultPlaceholderColorBrush == null) - _defaultPlaceholderColorBrush = Control.PlaceholderForegroundBrush; - - Control.PlaceholderForegroundBrush = placeholderColor.ToBrush(); + BrushHelpers.UpdateColor(Element.PlaceholderColor, ref _defaultPlaceholderColorBrush, + () => Control.PlaceholderForegroundBrush, brush => Control.PlaceholderForegroundBrush = brush); } void UpdateText() @@ -140,20 +128,12 @@ namespace Xamarin.Forms.Platform.WinPhone void UpdateTextColor() { - Color textColor = Element.TextColor; - - if (textColor.IsDefault) - { - if (_defaultTextColorBrush == null) - return; - - Control.Foreground = _defaultTextColorBrush; - } - - if (_defaultTextColorBrush == null) - _defaultTextColorBrush = Control.Foreground; - - Control.Foreground = textColor.ToBrush(); + BrushHelpers.UpdateColor(Element.TextColor, ref _defaultTextColorBrush, + () => Control.Foreground, brush => Control.Foreground = brush); + + // Force the PhoneTextBox control to do some internal bookkeeping + // so the colors change immediately and remain changed when the control gets focus + Control.OnApplyTemplate(); } } } \ No newline at end of file diff --git a/Xamarin.Forms.Platform.WP8/Xamarin.Forms.Platform.WP8.csproj b/Xamarin.Forms.Platform.WP8/Xamarin.Forms.Platform.WP8.csproj index eda2cdce..e78234f5 100644 --- a/Xamarin.Forms.Platform.WP8/Xamarin.Forms.Platform.WP8.csproj +++ b/Xamarin.Forms.Platform.WP8/Xamarin.Forms.Platform.WP8.csproj @@ -142,6 +142,7 @@ + -- cgit v1.2.3