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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using GalaSoft.MvvmLight.Command;
namespace FLY.ControlLibrary
{
/// <summary>
/// Window_Tip.xaml 的交互逻辑
/// </summary>
public partial class Window_Tip : Window
{
WdTipVm viewModel;
/// <summary>
///
/// </summary>
public Window_Tip()
{
InitializeComponent();
}
/// <summary>
/// 初始化
/// </summary>
/// <param name="title"></param>
/// <param name="message"></param>
/// <param name="duration"></param>
public void Init(string title, string message, TimeSpan duration)
{
if (viewModel == null)
{
viewModel = new WdTipVm();
DataContext = viewModel;
}
viewModel.Init(title, message, duration);
}
/// <summary>
/// 右下角 提示
/// </summary>
/// <param name="title">标题</param>
/// <param name="message">内容</param>
/// <param name="duration">持续时间</param>
public static void Show(string title, string message, TimeSpan duration)
{
WdTipVm.Show(title, message, duration);
}
/// <summary>
/// 右下角 短时间提示
/// </summary>
/// <param name="title">标题</param>
/// <param name="message">内容</param>
public static void ShowShortTime(string title, string message)
{
Show(title, message, TimeSpan.FromSeconds(2));
}
/// <summary>
/// 右下角 永远显示提示
/// </summary>
/// <param name="title">标题</param>
/// <param name="message">内容</param>
public static void Show(string title, string message)
{
Show(title, message, TimeSpan.MaxValue);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
viewModel.LoadedCmd.Execute(null);
}
}
class WdTipVm:INotifyPropertyChanged
{
private static Window_Tip Current=null;
/// <summary>
/// 持续时间
/// </summary>
public TimeSpan Duration { get; set; }
/// <summary>
/// 提示内容
/// </summary>
public string Message { get; set; }
/// <summary>
/// 标题
/// </summary>
public string Title { get; set; }
/// <summary>
/// 加载事件
/// </summary>
public RelayCommand LoadedCmd { get; private set; }
/// <summary>
/// 关闭事件
/// </summary>
public RelayCommand CloseCmd { get; private set; }
public event PropertyChangedEventHandler PropertyChanged;
System.Windows.Threading.DispatcherTimer timer;
public WdTipVm()
{
timer = new System.Windows.Threading.DispatcherTimer();
timer.Tick += new EventHandler(timer_Tick);
InitCmd();
}
public void Init(string title, string message, TimeSpan duration)
{
Title = title;
Message = message;
Duration = duration;
if (!Current.IsVisible)
Current.Show();
if (duration < TimeSpan.MaxValue && duration > TimeSpan.MinValue)
{
timer.Interval = duration;
timer.Start();
}
else
{
if (timer.IsEnabled)
timer.Stop();
}
}
void InitCmd()
{
LoadedCmd = new RelayCommand(() =>
{
if (Current == null)
return;//不正常
Current.Left = SystemParameters.PrimaryScreenWidth - Current.Width;
Current.Top = SystemParameters.PrimaryScreenHeight - Current.Height - 100;
if (Duration < TimeSpan.MaxValue && Duration > TimeSpan.MinValue)
{
timer.Interval = Duration;
timer.Start();
}
});
CloseCmd = new RelayCommand(() =>
{
if (Current == null)
return;//不正常
timer_Tick(null, null);
});
}
void timer_Tick(object sender, EventArgs e)
{
timer.Stop();
Current.Close();
Current = null;
}
/// <summary>
/// 右下角 提示
/// </summary>
/// <param name="title">标题</param>
/// <param name="message">内容</param>
/// <param name="duration">持续时间</param>
public static void Show(string title, string message, TimeSpan duration)
{
if (Current == null)
Current = new Window_Tip();
Current.Init(title, message, duration);
}
}
}