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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
namespace FLY.FeedbackRenZiJia.UI.Client.UiModule
{
public class LongPress : INotifyPropertyChanged
{
/// <summary>
/// 满9
/// </summary>
public int Progress { get; set; }
public bool IsOK => (Progress == Max);
public bool IsRunning => (Progress > 0);
/// <summary>
/// Progress 最大值
/// </summary>
public int Max { get; set; } = 360;
/// <summary>
/// 时间间隔
/// </summary>
public int IntervalMs { get; set; } = 2000;
public RoutedEventHandler LongClick;
private Stopwatch stopwatch = new Stopwatch();
DispatcherTimer t = new DispatcherTimer();
DispatcherTimer t_reset = new DispatcherTimer();
public LongPress()
{
t.Tick += T_Tick;
t.Interval = TimeSpan.FromSeconds(0.1);
t_reset.Tick += T_reset_Tick;
t.Interval = TimeSpan.FromSeconds(0.1);
}
private void T_reset_Tick(object sender, EventArgs e)
{
Reset();
t_reset.Stop();
}
UIElement element;
public void Init(UIElement element)
{
element.PreviewMouseDown += (s, e) => { button_MouseDown(); };
element.PreviewTouchDown += (s, e) => { button_MouseDown(); };
element.PreviewMouseUp += (s, e) => { button_MouseUp(); };
element.PreviewTouchUp += (s, e) => { button_MouseUp(); };
this.element = element;
}
public void button_MouseDown()
{
//必须点3秒
Progress = 0;
stopwatch.Restart();
t.Start();
}
public void button_MouseUp()
{
t.Stop();
t_reset.Start();
}
private void T_Tick(object sender, EventArgs e)
{
TimeSpan ts = stopwatch.Elapsed;
double p = (double)(Max) * ts.TotalMilliseconds / IntervalMs;
if (p >= Max)
{
Progress = Max;
stopwatch.Stop();
t.Stop();
LongClick?.Invoke(element, null);
}
else
{
Progress = (int)p;
}
}
public void Reset()
{
Progress = 0;
}
#region INotifyPropertyChanged 成员
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
}