-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelloWorld.cs
100 lines (89 loc) · 2.87 KB
/
HelloWorld.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
// The Templated Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234235
namespace CustomControls
{
public sealed class HelloWorld : Control
{
public HelloWorld()
{
this.DefaultStyleKey = typeof(HelloWorld);
}
public bool Blink
{
get { return (bool)GetValue(BlinkProperty); }
set { SetValue(BlinkProperty, value); }
}
// Using a DependencyProperty enables animation, styling, binding, etc.
public static readonly DependencyProperty BlinkProperty =
DependencyProperty.Register(
"Blink", // The name of the DependencyProperty
typeof(bool), // The type of the DependencyProperty
typeof(HelloWorld), // The type of the owner of the DependencyProperty
new PropertyMetadata( // OnBlinkChanged will be called when Blink changes
false, // The default value of the DependencyProperty
new PropertyChangedCallback(OnBlinkChanged)
)
);
private DispatcherTimer __timer = null;
private DispatcherTimer _timer
{
get
{
if (__timer == null)
{
__timer = new DispatcherTimer();
__timer.Interval = new TimeSpan(0,0,0,0,500); // 500 ms interval
__timer.Tick += __timer_Tick;
}
return __timer;
}
}
private static void OnBlinkChanged(
DependencyObject d,
DependencyPropertyChangedEventArgs e
)
{
var instance = d as HelloWorld;
if (instance != null)
{
if (instance._timer.IsEnabled != instance.Blink)
{
if (instance.Blink)
{
instance._timer.Start();
}
else
{
instance._timer.Stop();
}
}
}
}
private void __timer_Tick(object sender, object e)
{
DoBlink();
}
public void DoBlink()
{
this.Opacity = (this.Opacity + 1) % 2;
OnBlinked();
}
public event EventHandler Blinked;
private void OnBlinked()
{
EventHandler eh = Blinked;
if (eh != null)
{
eh(this, new EventArgs());
}
}
}
}