My Silverlight系列(9)—— 模拟Silverlight中的双击事件
2009-04-25 12:04:39 来源:WEB开发网 1 public class DoubleClickHelper
2 {
3 Constant#region Constant
4 private const long m_DoubleClickInterval = 2000000;
5 #endregion
6
7 Events#region Events
8 public event EventHandler<MouseButtonEventArgs> DoubleClick;
9 public event EventHandler<MouseButtonEventArgs> Click;
10 #endregion Events
11
12 Field#region Field
13 private DispatcherTimer m_Timer;
14 private bool m_IsSpecial;
15 private FrameworkElement m_Element;
16 private long m_DoubleClickTicks = 0;
17 private object Temp_Sender;
18 private MouseButtonEventArgs Temp_EventArgs;
19 #endregion
20
21 Property#region Property
22 private FrameworkElement Element
23 {
24 get { return m_Element; }
25 set
26 {
27 if (value != null && m_Element != value)
28 {
29 m_Element = value;
30 m_Element.MouseLeftButtonDown += new MouseButtonEventHandler(OnMouseLeftButtonDown);
31 }
32 }
33 }
34
35 public bool IsSpecial
36 {
37 get { return m_IsSpecial; }
38 set
39 {
40 m_IsSpecial = value;
41 if (IsSpecial)
42 {
43 this.m_Timer = new DispatcherTimer();
44 this.m_Timer.Interval = TimeSpan.FromTicks(m_DoubleClickInterval);
45 this.m_Timer.Tick += new EventHandler(m_Timer_Tick);
46 }
47 }
48 }
49 #endregion
50
51 Construstor#region Construstor
52 public DoubleClickHelper(FrameworkElement Element)
53 : this(Element, false) { }
54
55
56 public DoubleClickHelper(FrameworkElement Element, bool IsSpecial)
57 {
58 this.Element = Element;
59 this.IsSpecial = IsSpecial;
60 }
61 #endregion
62
63 Event Handlers#region Event Handlers
64 void m_Timer_Tick(object sender, EventArgs e)
65 {
66 RaiseSingleClick(Temp_Sender, Temp_EventArgs);
67 m_Timer.Stop();
68 }
69
70 void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
71 {
72 if (IsSpecial)
73 {
74 Temp_Sender = sender;
75 Temp_EventArgs = e;
76 m_Timer.Start();
77 }
78
79 if (DateTime.Now.Ticks - m_DoubleClickTicks < m_DoubleClickInterval)
80 {
81 if (IsSpecial)
82 m_Timer.Stop();
83 RaiseDoubleClick(sender, e);
84 }
85 else
86 {
87 if (!IsSpecial)
88 RaiseSingleClick(sender, e);
89 }
90 m_DoubleClickTicks = DateTime.Now.Ticks;
91 }
92 #endregion
93
94 Raise Events#region Raise Events
95 void RaiseDoubleClick(object sender, MouseButtonEventArgs e)
96 {
97 if (DoubleClick != null)
98 DoubleClick(sender, e);
99 }
100
101 void RaiseSingleClick(object sender, MouseButtonEventArgs e)
102 {
103 if (Click != null)
104 Click(sender, e);
105 }
106 #endregion
107 }
Tags:My Silverlight 系列
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接