黄学彪
2021-01-28 1fcf2302f79f9cbea5ef17c3688311ed65cfabb4
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
using Shared;
using HDL_ON.UI.CSS;
using System;
using System.Text;
 
namespace HDL_ON.Stan
{
    /// <summary>
    /// 显示一个信息框(请使用HdlMessageLogic调用)
    /// </summary>
    public class ShowMsgControl
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 点击确认的事件
        /// </summary>
        public Action ConfirmClickEvent = null;
        /// <summary>
        /// 点击取消的事件
        /// </summary>
        public Action CancelClickEvent = null;
        /// <summary>
        /// 信息类型
        /// </summary>
        private ShowMsgType msgType = ShowMsgType.Confirm;
        /// <summary>
        /// 消息
        /// </summary>
        private string msgText = string.Empty;
        /// <summary>
        /// 确认按钮的文本
        /// </summary>
        private string buttonOkText = null;
        /// <summary>
        /// 取消按钮的文本
        /// </summary>
        private string buttonCancelText = null;
        /// <summary>
        /// 提示控件
        /// </summary>
        private Tip myTip = null;
        /// <summary>
        /// 等待时间
        /// </summary>
        private int WaitTime = -1;
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 显示一个需要确认的信息框
        /// </summary>
        /// <param name="i_msgType">信息类型</param>
        /// <param name="i_msg">信息</param>
        /// <param name="i_buttonOkText">确认按钮的文本</param>
        /// <param name="i_buttonCancelText">取消按钮的文本</param>
        /// <param name="i_waitTime">等待时间,单位为秒,设置确认按钮在多长时间后才能够点击</param>
        public ShowMsgControl(ShowMsgType i_msgType, string i_msg, string i_buttonOkText = null, string i_buttonCancelText = null, int i_waitTime = -1)
        {
            //确认按钮文本
            this.buttonOkText = i_buttonOkText == null ? Language.StringByID(StringId.Confirm) : i_buttonOkText;
            this.buttonCancelText = i_buttonCancelText == null ? Language.StringByID(StringId.Cancel) : i_buttonCancelText;
            this.msgType = i_msgType;
            this.msgText = i_msg;
            this.WaitTime = i_waitTime;
 
            if (i_msgType == ShowMsgType.Tip)
            {
                myTip = new Tip();
                myTip.Direction = AMPopTipDirection.None;
                myTip.CloseTime = 2;
                myTip.Text = i_msg;
            }
        }
 
        #endregion
 
        #region ■ 显示消息___________________________
 
        /// <summary>
        /// 显示
        /// </summary>
        public void Show()
        {
            try
            {
                if (myTip != null)
                {
                    myTip.Show(MainPage.BasePageView);
                    myTip = null;
                    return;
                }
                //初始化控件
                this.InitMsgControl();
            }
            catch { }
        }
 
        #endregion
 
        #region ■ 初始化控件_________________________
 
        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitMsgControl()
        {
            var dialogForm = new Dialog();
            dialogForm.BackgroundColor = CSS_Color.DialogTransparentColor1;
            //主控件
            var frameMain = new NormalFrameLayout();
            dialogForm.AddChidren(frameMain);
            dialogForm.Show();
 
            //计算用
            var btnTemp = new ButtonCtrBase();
            btnTemp.TextSize = CSS_FontSize.PromptFontSize_FirstLevel;
            btnTemp.Text = msgText.Replace("\r\n", string.Empty);
            //获取这个显示的内容的高度
            int rowCount = btnTemp.GetRealRowCountByText();
            int contentHeight = rowCount * Application.GetRealHeight(18);
            if (rowCount <= 2)
            {
                //它有一个最小高度
                contentHeight = Application.GetRealHeight(51);
            }
 
            //这个区域的高度比例为:显示内容到上部的距离(50)+显示内容的高度+显示内容到底部的距离(70)
 
            //中间区域
            var frameCenter = new NormalFrameLayout();
            frameCenter.Gravity = Gravity.Center;
            frameCenter.Width = Application.GetRealWidth(270);
            frameCenter.Height = Application.GetRealHeight(50) + contentHeight + Application.GetRealHeight(70);
            frameCenter.BackgroundColor = CSS_Color.MainBackgroundColor;
            frameCenter.BorderColor = 0x00000000;
            frameCenter.BorderWidth = 0;
            frameCenter.Radius = (uint)Application.GetMinRealAverage(10);
            frameMain.AddChidren(frameCenter);
 
            //标题
            var btnTitle = new NormalViewControl(frameCenter.Width, Application.GetRealHeight(22), false);
            btnTitle.Y = Application.GetRealHeight(20);
            btnTitle.TextColor = CSS_Color.MainColor;
            btnTitle.TextSize = CSS_FontSize.SubheadingFontSize;
            btnTitle.TextAlignment = TextAlignment.Center;
            btnTitle.IsBold = true;
            btnTitle.Text = Language.StringByID(StringId.Tip);
            frameCenter.AddChidren(btnTitle);
            //提示内容按钮
            var btnMsg = new NormalViewControl(Application.GetRealWidth(258), contentHeight, false);
            btnMsg.Y = btnTitle.Bottom + Application.GetRealHeight(8);
            btnMsg.Gravity = Gravity.CenterHorizontal;
            btnMsg.TextAlignment = TextAlignment.Center;
            btnMsg.TextSize = CSS_FontSize.PromptFontSize_FirstLevel;
            btnMsg.Text = msgText;
            btnMsg.IsMoreLines = true;
            frameCenter.AddChidren(btnMsg);
 
            if (msgType == ShowMsgType.Confirm)
            {
                //初始化确认类型的底部按钮
                this.InitBottomConfirmButton(dialogForm, frameCenter);
            }
            else
            {
                //初始化普通类型的底部按钮
                this.InitBottomNormalButton(dialogForm, frameCenter);
            }
        }
 
        /// <summary>
        /// 初始化确认类型的底部按钮
        /// </summary>
        /// <param name="frameCenter"></param>
        private void InitBottomConfirmButton(Dialog dialogForm, NormalFrameLayout frameCenter)
        {
            //取消
            var btnCancel = new NormalViewControl(frameCenter.Width / 2, Application.GetRealHeight(43), false);
            btnCancel.Gravity = Gravity.BottomLeft;
            btnCancel.TextAlignment = TextAlignment.Center;
            btnCancel.TextSize = CSS_FontSize.SubheadingFontSize;
            btnCancel.Text = this.buttonCancelText;
            //btnCancel.SetCornerWithSameRadius((uint)Application.GetMinRealAverage(10), HDLUtils.RectCornerBottomLeft);
            frameCenter.AddChidren(btnCancel);
            btnCancel.ButtonClickEvent += (sender, e) =>
            {
                //关闭界面
                dialogForm.Close();
                //回调函数
                this.CancelClickEvent?.Invoke();
                this.ConfirmClickEvent = null;
                this.CancelClickEvent = null;
            };
            //线
            var btnLine = new NormalViewControl(frameCenter.Width / 2, HdlControlResourse.BottomLineHeight, false);
            btnLine.Y = btnCancel.Y - HdlControlResourse.BottomLineHeight;
            btnLine.BackgroundColor = CSS_Color.DividingLineColor;
            frameCenter.AddChidren(btnLine);
 
            //确认
            var btnConfirm = new NormalViewControl(frameCenter.Width - btnCancel.Width, Application.GetRealHeight(45), false);
            btnConfirm.X = btnCancel.Right;
            btnConfirm.Y = btnLine.Y;
            btnConfirm.TextAlignment = TextAlignment.Center;
            btnConfirm.TextSize = CSS_FontSize.SubheadingFontSize;
            btnConfirm.TextColor = CSS_Color.MainBackgroundColor;
            btnConfirm.BackgroundColor = CSS_Color.MainColor;
            btnConfirm.Text = this.buttonOkText;
            frameCenter.AddChidren(btnConfirm);
            btnConfirm.SetCornerWithSameRadius((uint)Application.GetMinRealAverage(10), HDLUtils.RectCornerBottomRight);
            btnConfirm.ButtonClickEvent += (sender, e) =>
            {
                //关闭界面
                dialogForm.Close();
                //回调函数
                this.ConfirmClickEvent?.Invoke();
                this.ConfirmClickEvent = null;
                this.CancelClickEvent = null;
            };
 
            //开启等待时间
            this.StartWaitTime(btnConfirm);
        }
 
        /// <summary>
        /// 初始化普通类型的底部按钮
        /// </summary>
        /// <param name="dialogForm"></param>
        /// <param name="frameCenter"></param>
        private void InitBottomNormalButton(Dialog dialogForm, NormalFrameLayout frameCenter)
        {
            //确认
            var btnConfirm = new NormalViewControl(frameCenter.Width, Application.GetRealHeight(45), false);
            btnConfirm.Gravity = Gravity.BottomCenter;
            btnConfirm.TextAlignment = TextAlignment.Center;
            btnConfirm.TextSize = CSS_FontSize.SubheadingFontSize;
            btnConfirm.TextColor = CSS_Color.MainBackgroundColor;
            btnConfirm.BackgroundColor = CSS_Color.MainColor;
            btnConfirm.Text = this.buttonOkText;
            frameCenter.AddChidren(btnConfirm);
            btnConfirm.SetCornerWithSameRadius((uint)Application.GetMinRealAverage(10), HDLUtils.RectCornerBottomLeft | HDLUtils.RectCornerBottomRight);
            btnConfirm.ButtonClickEvent += (sender, e) =>
            {
                //关闭界面
                dialogForm.Close();
                //回调函数
                this.ConfirmClickEvent?.Invoke();
                this.ConfirmClickEvent = null;
                this.CancelClickEvent = null;
            };
 
            //开启等待时间
            this.StartWaitTime(btnConfirm);
        }
 
        #endregion
 
        #region ■ 开启等待时间_______________________
 
        /// <summary>
        /// 开启等待时间(此函数只用于安卓)
        /// </summary>
        /// <param name="btnConfirm">确认按钮</param>
        private void StartWaitTime(NormalViewControl btnConfirm)
        {
            if (this.WaitTime <= 0)
            {
                return;
            }
 
            btnConfirm.CanClick = false;
            HdlThreadLogic.Current.RunThread(() =>
            {
                //显示剩余等待时间
                while (btnConfirm.Parent != null && this.WaitTime >= 0)
                {
                    HdlThreadLogic.Current.RunMain(() =>
                    {
                        btnConfirm.Text = this.buttonOkText + "(" + this.WaitTime + ")";
 
                    }, ShowErrorMode.NO);
                    System.Threading.Thread.Sleep(1000);
                    this.WaitTime--;
                }
                HdlThreadLogic.Current.RunMain(() =>
                {
                    //可以点击
                    btnConfirm.Text = this.buttonOkText;
                    btnConfirm.CanClick = true;
 
                }, ShowErrorMode.NO);
            });
        }
 
        /// <summary>
        /// 开启等待时间
        /// </summary>
        /// <param name="btnConfirm">确认按钮</param>
        private void StartWaitTime(ButtonCtrBase btnConfirm)
        {
            if (this.WaitTime <= 0)
            {
                return;
            }
            //不能点击
            btnConfirm.CanClick = false;
 
            HdlThreadLogic.Current.RunThread(() =>
            {
                //显示剩余等待时间
                while (btnConfirm.Parent != null && this.WaitTime >= 0)
                {
                    HdlThreadLogic.Current.RunMain(() =>
                    {
                        btnConfirm.Text = this.buttonOkText + "(" + this.WaitTime + ")";
                    }, ShowErrorMode.NO);
                    System.Threading.Thread.Sleep(1000);
                    this.WaitTime--;
                }
                HdlThreadLogic.Current.RunMain(() =>
                {
                    //可以点击
                    btnConfirm.Text = this.buttonOkText;
                    btnConfirm.CanClick = true;
 
                }, ShowErrorMode.NO);
            });
        }
 
        #endregion
    }
}