summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla44955.cs
blob: a30a65bd47a9c608dc56bdd8b2389ccedda12c93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System.Diagnostics;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

#if UITEST
using Xamarin.UITest;
using NUnit.Framework;
#endif

namespace Xamarin.Forms.Controls.Issues
{
	[Preserve(AllMembers = true)]
	[Issue(IssueTracker.Bugzilla, 44955, "[WinRT/UWP] Setting Entry BackgroundColor via Behavior results in sticky unfocused background color", PlatformAffected.WinRT)]
	public class Bugzilla44955 : TestContentPage
	{
		Entry _validationEntry;
		protected override void Init()
		{
			_validationEntry = new Entry();
			_validationEntry.Behaviors.Add(new NonEmptyStringValidator());
			Content = new StackLayout
			{
				Children =
				{
					new Label
					{
						Text = "The first entry should have a red background only when it is empty, regardless of focus (due to an attached behavior). The second has a set background color, the third is default, and the last is default, but disabled."
					},
					_validationEntry,
					new Entry
					{
						BackgroundColor = Color.MediumPurple
					},
					new Entry(),
					new Entry
					{
						IsEnabled = false
					},
					new Button
					{
						Text = "Change background of first label to yellow",
						Command = new Command(() => _validationEntry.BackgroundColor = Color.Yellow)
					}
				}
			};
		}

		protected override void OnDisappearing()
		{
			base.OnDisappearing();
			_validationEntry.Behaviors.Clear();
		}

		class NonEmptyStringValidator : Behavior<Entry>
		{
			protected override void OnAttachedTo(Entry bindable)
			{
				bindable.TextChanged += HandleTextChanged;
				Validate(bindable, bindable.Text);
			}

			protected override void OnDetachingFrom(Entry bindable)
			{
				bindable.TextChanged -= HandleTextChanged;
			}

			void HandleTextChanged(object sender, TextChangedEventArgs e)
			{
				Validate((Entry)sender, e.NewTextValue);
			}

			void Validate(Entry entry, string text)
			{
				if (text == null)
					entry.BackgroundColor = Color.Red;
				else
					entry.BackgroundColor = text.Trim() != "" ? Color.Default : Color.Red;
			}
		}
	}
}