Bug 42680 - UWP - Entry: changing the background color will only take effect after the entry gained focus.
Summary: UWP - Entry: changing the background color will only take effect after the en...
Alias: None
Product: Forms
Classification: Xamarin
Component: Windows ()
Version: 2.3.1
Hardware: Other Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2016-07-20 06:12 UTC by georg.haberl
Modified: 2016-11-25 12:17 UTC (History)
4 users (show)

Tags: Windows ac
Is this bug a regression?: ---
Last known good build:

Repro project (6.84 MB, application/x-zip-compressed)
2016-08-11 15:35 UTC, E.Z. Hart [MSFT]

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description georg.haberl 2016-07-20 06:12:16 UTC
When changing the background color on a entry nothing will happen until the entry will gain focus -> if it is already focused it must be unfocused and focused again.

The bug is in the FormsTextBox class, when changing the background only a property is set (BackgroundFocusBrush).
Once "OnGotFocus" is called the property value (BackgroundFocusBrush) is applied to the "BorderElement".

The reason for this was a problem when setting the background color on a focused entry:

//If we're on the phone, the Visual State Manager crashes if we try to 
// handle alternate background colors in the focus state; we have to do
// it manually here"

So I figured maybe this would work now so i created my own renderer and used reflection to update the value on every "UpdateBackgroundColor" call.

protected override void UpdateBackgroundColor()
 if (Control == null)
 var info = typeof(FormsTextBox).GetField("_borderElement", BindingFlags.NonPublic | BindingFlags.Instance);
 var _borderElement = info.GetValue(Control) as Border;
 if (_borderElement != null)
  _borderElement.Background = Control.BackgroundFocusBrush;

This works perfectly fine for me on Lumia 640 XL -> and updates even during editing the contents.
Are ther still cases when this would cause a crash? If not the border element should be updated normally instead of using another property to update it with "OnGotFocus".
If the problem persists, it should update on some other cases as well, right now I'm  using the Entry coloring as validator -> i.e. when an input is mandatory the entry is yellow when it's empty... so with my change it will lose/gain the color while editing. Without the hack this won't work at all.
Comment 1 E.Z. Hart [MSFT] 2016-08-11 15:35:17 UTC
Created attachment 16990 [details]
Repro project
Comment 2 E.Z. Hart [MSFT] 2016-08-11 15:41:51 UTC
Added a repro project to demonstrate the issue. Everything works as expected on the desktop (minus the problems described in https://bugzilla.xamarin.com/show_bug.cgi?id=43282), but the phone gets into a broken state once the Entry gains focus.

In the repro project, the toggle button works fine (changes the background between green and default) until the Entry gains focus; after that, the toggle stops working. Also, in the repro project the background of the Entry should turn red as soon as the user types a "4" anywhere in the box; this does not work on the phone.
Comment 3 Karol S 2016-09-21 12:26:59 UTC
I can see that it was fixed in version 2.3.2-pre2 for Entry but it still does not work for Editor (that is almost he same as entry)
Comment 4 E.Z. Hart [MSFT] 2016-09-21 16:38:06 UTC
(In reply to Karol S from comment #3)
> I can see that it was fixed in version 2.3.2-pre2 for Entry but it still
> does not work for Editor (that is almost he same as entry)

I've created bug 44584 for the Editor version of this issue.
Comment 5 E.Z. Hart [MSFT] 2016-10-14 15:36:02 UTC
Should be fixed in 2.3.3-pre3
Comment 6 E.Z. Hart [MSFT] 2016-10-14 16:49:40 UTC
Sorry, meant that it should be fixed in 2.3.2-pre2
Comment 7 Parmendra Kumar 2016-11-25 12:17:28 UTC
I have checked this issue with X.F 2.3.4-pre1 and its working fine.

Hence closing this issue.