summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WinRT/FormsTimePicker.cs
blob: c135a585689a7c5e3b9aae3b4a81d0e5921d2919 (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
using System;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

#if WINDOWS_UWP

namespace Xamarin.Forms.Platform.UWP
#else

namespace Xamarin.Forms.Platform.WinRT
#endif
{
	public class FormsTimePicker : Windows.UI.Xaml.Controls.TimePicker
	{
		public FormsTimePicker()
		{
			if (Device.Idiom == TargetIdiom.Desktop || Device.Idiom == TargetIdiom.Tablet)
			{
				Loaded += (sender, args) => { Window.Current.Activated += WindowOnActivated; };
				Unloaded += (sender, args) => { Window.Current.Activated -= WindowOnActivated; };
			}
		}

		public event EventHandler<EventArgs> ForceInvalidate;

		protected override void OnApplyTemplate()
		{
			base.OnApplyTemplate();

			if (Device.Idiom == TargetIdiom.Desktop || Device.Idiom == TargetIdiom.Tablet)
			{
				// Look for the combo boxes which make up a TimePicker on Windows 8.1
				// So we can hook into their closed events and invalidate them if necessary

				var hourPicker = GetTemplateChild("HourPicker") as ComboBox;
				if (hourPicker != null)
				{
					hourPicker.DropDownClosed += PickerOnDropDownClosed;
				}

				var minutePicker = GetTemplateChild("MinutePicker") as ComboBox;
				if (minutePicker != null)
				{
					minutePicker.DropDownClosed += PickerOnDropDownClosed;
				}

				var periodPicker = GetTemplateChild("PeriodPicker") as ComboBox;
				if (periodPicker != null)
				{
					periodPicker.DropDownClosed += PickerOnDropDownClosed;
				}
			}
		}

		void PickerOnDropDownClosed(object sender, object o)
		{
			// If the TimePicker is in a TableView or ListView and the user 
			// opens one of the dropdowns but does not actually change the value,
			// when the dropdown closes, the selected value will go blank
			// To fix this, we have to invalidate the control
			// This only applies to Windows 8.1
			ForceInvalidate?.Invoke(this, EventArgs.Empty);
		}

		void WindowOnActivated(object sender, WindowActivatedEventArgs windowActivatedEventArgs)
		{
			// If the TimePicker is in a TableView or ListView, when the application loses focus
			// the TextBlock/ComboBox controls (UWP and 8.1, respectively) which display its selected value
			// will go blank.
			// To fix this, we have to signal the renderer to invalidate if
			// Window.Activated occurs.
			ForceInvalidate?.Invoke(this, EventArgs.Empty);
		}
	}
}