From dbd4390c5bf22a3f43a0f2f9b5defe4a76c727f4 Mon Sep 17 00:00:00 2001 From: "E.Z. Hart" Date: Thu, 20 Oct 2016 14:19:27 -0600 Subject: [PATCH] Make Editor focused background color behavior consistent with Entry on Windows (#456) --- .../Bugzilla44584.cs | 44 ++++++++++++++++++++++ .../Xamarin.Forms.Controls.Issues.Shared.projitems | 1 + Xamarin.Forms.Platform.WinRT/EditorRenderer.cs | 29 ++++++++++++-- Xamarin.Forms.Platform.WinRT/FormsTextBox.cs | 2 +- 4 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla44584.cs diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla44584.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla44584.cs new file mode 100644 index 0000000..f20edd7 --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla44584.cs @@ -0,0 +1,44 @@ +using Xamarin.Forms.CustomAttributes; +using Xamarin.Forms.Internals; + +namespace Xamarin.Forms.Controls +{ + [Preserve(AllMembers = true)] + [Issue(IssueTracker.Bugzilla, 44584, + "UWP - Editor: changing the background color will only take effect after the entry gained focus")] + public class Bugzilla44584 : TestContentPage + { + protected override void Init() + { + var instructions = new Label + { + Text = @" +Tap the first button once to turn the Entry background color to Green. Tap the Entry to focus it; the background should remain green; if it does not, the test has failed. +Tap the second button once to turn the Editor background color to Green. Tap the Editor to focus it; the background should remain green; if it does not, the test has failed." + }; + + var entryButton = new Button { Text = "Toggle Entry Background (Green/Default)" }; + var entry = new Entry(); + + entryButton.Clicked += + (sender, args) => { entry.BackgroundColor = entry.BackgroundColor != Color.Green ? Color.Green : Color.Default; }; + + var editorButton = new Button { Text = "Toggle Editor Background (Green/Default)" }; + var editor = new Editor() + { + HeightRequest = 80 + }; + + editorButton.Clicked += + (sender, args) => { editor.BackgroundColor = editor.BackgroundColor != Color.Green ? Color.Green : Color.Default; }; + + var layout = new StackLayout + { + VerticalOptions = LayoutOptions.Center, + Children = { instructions, entryButton, entry, editorButton, editor } + }; + + Content = layout; + } + } +} \ No newline at end of file diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems index edbe9d0..07b8d7b 100644 --- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems @@ -128,6 +128,7 @@ + diff --git a/Xamarin.Forms.Platform.WinRT/EditorRenderer.cs b/Xamarin.Forms.Platform.WinRT/EditorRenderer.cs index 7c4acfb..2fc0133 100644 --- a/Xamarin.Forms.Platform.WinRT/EditorRenderer.cs +++ b/Xamarin.Forms.Platform.WinRT/EditorRenderer.cs @@ -1,6 +1,7 @@ using System.ComponentModel; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; +using Windows.UI.Xaml.Media; #if WINDOWS_UWP @@ -10,9 +11,10 @@ namespace Xamarin.Forms.Platform.UWP namespace Xamarin.Forms.Platform.WinRT #endif { - public class EditorRenderer : ViewRenderer + public class EditorRenderer : ViewRenderer { bool _fontApplied; + Brush _backgroundColorFocusedDefaultBrush; protected override void OnElementChanged(ElementChangedEventArgs e) { @@ -20,7 +22,12 @@ namespace Xamarin.Forms.Platform.WinRT { if (Control == null) { - var textBox = new TextBox { AcceptsReturn = true, TextWrapping = TextWrapping.Wrap }; + var textBox = new FormsTextBox + { + AcceptsReturn = true, + TextWrapping = TextWrapping.Wrap, + Style = Windows.UI.Xaml.Application.Current.Resources["FormsTextBoxStyle"] as Windows.UI.Xaml.Style + }; SetNativeControl(textBox); @@ -50,6 +57,8 @@ namespace Xamarin.Forms.Platform.WinRT protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { + base.OnElementPropertyChanged(sender, e); + if (e.PropertyName == Editor.TextColorProperty.PropertyName) { UpdateTextColor(); @@ -70,8 +79,6 @@ namespace Xamarin.Forms.Platform.WinRT { UpdateText(); } - - base.OnElementPropertyChanged(sender, e); } void OnLostFocus(object sender, RoutedEventArgs e) @@ -79,6 +86,20 @@ namespace Xamarin.Forms.Platform.WinRT Element.SendCompleted(); } + protected override void UpdateBackgroundColor() + { + base.UpdateBackgroundColor(); + + if (Control == null) + { + return; + } + + // By default some platforms have alternate default background colors when focused + BrushHelpers.UpdateColor(Element.BackgroundColor, ref _backgroundColorFocusedDefaultBrush, + () => Control.BackgroundFocusBrush, brush => Control.BackgroundFocusBrush = brush); + } + void OnNativeTextChanged(object sender, Windows.UI.Xaml.Controls.TextChangedEventArgs args) { Element.SetValueCore(Editor.TextProperty, Control.Text); diff --git a/Xamarin.Forms.Platform.WinRT/FormsTextBox.cs b/Xamarin.Forms.Platform.WinRT/FormsTextBox.cs index 5847e0b..593c575 100644 --- a/Xamarin.Forms.Platform.WinRT/FormsTextBox.cs +++ b/Xamarin.Forms.Platform.WinRT/FormsTextBox.cs @@ -111,7 +111,7 @@ namespace Xamarin.Forms.Platform.WinRT if (Device.Idiom == TargetIdiom.Phone) { // If we're on the phone, we need to grab this from the template - // so we can manually handle it's background when focused + // so we can manually handle its background when focused _borderElement = (Border)GetTemplateChild("BorderElement"); } } -- 2.7.4