summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla41054.cs
blob: 27dddfe3d4cd50d24f808be97dde707e4bc9359f (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
using System.Linq;

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

// Apply the default category of "Issues" to all of the tests in this assembly
// We use this as a catch-all for tests which haven't been individually categorized
#if UITEST
[assembly: NUnit.Framework.Category("Issues")]
#endif

namespace Xamarin.Forms.Controls.Issues
{
	public class Bugzilla41054NumericValidationBehavior : Behavior<Entry>
	{
		public static readonly BindableProperty AttachBehaviorProperty =
			BindableProperty.CreateAttached("AttachBehavior", typeof(bool), typeof(Bugzilla41054NumericValidationBehavior), false, propertyChanged: OnAttachBehaviorChanged);

		public static bool GetAttachBehavior(BindableObject view)
		{
			return (bool)view.GetValue(AttachBehaviorProperty);
		}

		public static void SetAttachBehavior(BindableObject view, bool value)
		{
			view.SetValue(AttachBehaviorProperty, value);
		}

		static void OnAttachBehaviorChanged(BindableObject view, object oldValue, object newValue)
		{
			var entry = view as Entry;
			if (entry == null)
			{
				return;
			}

			bool attachBehavior = (bool)newValue;
			if (attachBehavior)
			{
				entry.Behaviors.Add(new Bugzilla41054NumericValidationBehavior());
			}
			else
			{
				var toRemove = entry.Behaviors.FirstOrDefault(b => b is Bugzilla41054NumericValidationBehavior);
				if (toRemove != null)
				{
					entry.Behaviors.Remove(toRemove);
				}
			}
		}

		protected override void OnAttachedTo(Entry entry)
		{
			entry.TextChanged += OnEntryTextChanged;
			base.OnAttachedTo(entry);
		}

		protected override void OnDetachingFrom(Entry entry)
		{
			entry.TextChanged -= OnEntryTextChanged;
			base.OnDetachingFrom(entry);
		}

		void OnEntryTextChanged(object sender, TextChangedEventArgs args)
		{
			double result;
			bool isValid = double.TryParse(args.NewTextValue, out result);
			((Entry)sender).TextColor = isValid ? Color.Default : Color.Red;
		}
	}

	[Preserve(AllMembers = true)]
	[Issue(IssueTracker.Bugzilla, 41054, "Cannot update Entry.Text from attached Behavior on UWP", PlatformAffected.Default)]
	public class Bugzilla41054 : TestContentPage
	{
		protected override void Init()
		{
			var entry = new Entry
			{
				Placeholder = "Enter a System.Double; text will be red when invalid",
				PlaceholderColor = Color.Green,
			};
			var entry2 = new Entry
			{
				Placeholder = "This entry starts with blue text when typing",
				TextColor = Color.Blue
			};

			Bugzilla41054NumericValidationBehavior.SetAttachBehavior(entry, true);

			Content = new StackLayout
			{
				Children = {
					entry,
					entry2,
					new Entry
					{
						Text = "This is an entry with some default colored text"
					},
					new Button
					{
						Text = "Change first entry placeholder color to purple",
						Command = new Command(() => entry.PlaceholderColor = Color.Purple)
					},
					new Button
					{
						Text = "Change second entry text color to orange",
						Command = new Command(() => entry2.TextColor = Color.Orange)
					}
				}
			};
		}
	}
}