黄学彪
2020-12-16 0d9f64668fd7350d6a21fd157e32009a96d98134
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
using System;
using System.Collections.Generic;
using System.Text;
 
namespace Shared.Phone
{
    /// <summary>
    /// 做成一个点击后能够显示点击状态的控件(基层控件)
    /// </summary>
    public class FrameLayoutStatuControl : FrameLayoutBase
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 状态设置的事件(会重载底层效果)
        /// </summary>
        public Action<bool> SelectStatuEvent;
        /// <summary>
        /// 是否启用点亮功能(默认启用)
        /// </summary>
        public bool UseClickStatu = true;
        /// <summary>
        /// 原来的背景色
        /// </summary>
        private uint oldBackColor = 0;
        /// <summary>
        /// 当前是否已经处于选择状态
        /// </summary>
        private bool IsSelectStatu = false;
        /// <summary>
        /// 子控件Y轴偏移量(共通定义而已)
        /// </summary>
        public int chidrenYaxis = 0;
        /// <summary>
        /// 圆角度
        /// </summary>
        public int RadiusEx
        {
            set { this.Radius = (uint)Application.GetRealHeight(value); }
        }
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 做成一个点击后能够显示点击状态的控件
        /// </summary>
        /// <param name="i_ChidrenYaxis">子控件Y轴偏移量(【列表控件的rowSpace/2】即可,不懂默认为0即可)</param>
        public FrameLayoutStatuControl(int i_ChidrenYaxis = 0)
        {
            this.chidrenYaxis = i_ChidrenYaxis;
 
            //置空底层的事件
            this.MouseUpEventHandler = null;
            this.MouseUpEventHandler += ChildrenUpEvent;
            this.MouseDownEventHandler += ChildrenDownEvent;
        }
 
        #endregion
 
        #region ■ 绑定事件___________________________
 
        /// <summary>
        /// 变更子控件的绑定模式
        /// </summary>
        /// <param name="view">子控件</param>
        /// <param name="chidrenBindMode">变更的绑定模式</param>
        public void ChangedChidrenBindMode(View view, ChidrenBindMode chidrenBindMode)
        {
            if (view is ButtonBase)
            {
                //子控件移除事件
                ButtonBase button = (ButtonBase)view;
                button.ButtonClickEvent -= ChildrenUpEvent;
                button.MouseDownEventHandler -= ChildrenDownEvent;
 
                this.BindChidrenEvent(view, chidrenBindMode);
            }
            else if (view is ImageView)
            {
                view.MouseUpEventHandler -= ChildrenUpEvent;
                view.MouseDownEventHandler -= ChildrenDownEvent;
 
                this.BindChidrenEvent(view, chidrenBindMode);
            }
            else if (view is ViewGroup)
            {
                ViewGroup groupContr = (ViewGroup)view;
                for (int i = 0; i < groupContr.ChildrenCount; i++)
                {
                    var myView = groupContr.GetChildren(i);
                    if (myView == null)
                    {
                        break;
                    }
                    if (myView is ButtonBase)
                    {
                        //子控件移除事件
                        ButtonBase button = (ButtonBase)myView;
                        button.ButtonClickEvent -= ChildrenUpEvent;
                        button.MouseDownEventHandler -= ChildrenDownEvent;
                    }
                }
                //自身移除事件
                groupContr.MouseUpEventHandler -= ChildrenUpEvent;
                groupContr.MouseDownEventHandler -= ChildrenDownEvent;
 
                this.BindChidrenEvent(view, chidrenBindMode);
            }
        }
 
        /// <summary>
        /// 绑定子控件事件(如果是复合控件,在初始化完成后,调用ChangedChidrenBindMode)
        /// </summary>
        /// <param name="view"></param>
        /// <param name="chidrenBindMode"></param>
        private void BindChidrenEvent(View view, ChidrenBindMode chidrenBindMode)
        {
            if (chidrenBindMode == ChidrenBindMode.NotBind)
            {
                return;
            }
            if (view is ButtonBase)
            {
                //为子控件添加事件
                ButtonBase button = (ButtonBase)view;
                button.ButtonClickEvent -= ChildrenUpEvent;
                button.MouseDownEventHandler -= ChildrenDownEvent;
 
                button.ButtonClickEvent += ChildrenUpEvent;
                button.MouseDownEventHandler += ChildrenDownEvent;
            }
            else if (view is ImageView)
            {
                //自身也添加事件
                view.MouseUpEventHandler -= ChildrenUpEvent;
                view.MouseDownEventHandler -= ChildrenDownEvent;
 
                view.MouseUpEventHandler += ChildrenUpEvent;
                view.MouseDownEventHandler += ChildrenDownEvent;
            }
            else if (view is ViewGroup)
            {
                //为子控件添加事件
                ViewGroup groupContr = (ViewGroup)view;
                for (int i = 0; i < groupContr.ChildrenCount; i++)
                {
                    var myView = groupContr.GetChildren(i);
                    if (myView == null)
                    {
                        break;
                    }
                    if (myView is ButtonBase)
                    {
                        //为子控件添加事件
                        ButtonBase button = (ButtonBase)myView;
                        button.ButtonClickEvent -= ChildrenUpEvent;
                        button.MouseDownEventHandler -= ChildrenDownEvent;
 
                        button.ButtonClickEvent += ChildrenUpEvent;
                        button.MouseDownEventHandler += ChildrenDownEvent;
                    }
                }
                //自身也添加事件
                groupContr.MouseUpEventHandler -= ChildrenUpEvent;
                groupContr.MouseDownEventHandler -= ChildrenDownEvent;
 
                groupContr.MouseUpEventHandler += ChildrenUpEvent;
                groupContr.MouseDownEventHandler += ChildrenDownEvent;
            }
        }
 
        #endregion
 
        #region ■ 添加子控件_________________________
 
        /// <summary>
        /// 添加子控件
        /// </summary>
        /// <param name="view">子控件</param>
        /// <param name="chidrenBindMode">绑定模式</param>
        public void AddChidren(View view, ChidrenBindMode chidrenBindMode = ChidrenBindMode.BindEvent)
        {
            this.oldBackColor = this.BackgroundColor;
 
            base.AddChidren(view);
 
            //绑定子控件事件
            this.BindChidrenEvent(view, chidrenBindMode);
        }
 
        /// <summary>
        /// 添加子控件
        /// </summary>
        /// <param name="view"></param>
        public override void AddChidren(View view)
        {
            this.AddChidren(view, ChidrenBindMode.NotBind);
        }
 
        #endregion
 
        #region ■ 控件事件___________________________
 
        /// <summary>
        /// 点击按下事件(点亮)
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">E.</param>
        private void ChildrenDownEvent(object sender, MouseEventArgs e)
        {
            if (this.UseClickStatu == false || this.CanClick == false)
            {
                return;
            }
            this.StartSelectStatuAppeal(HdlControlResourse.StatuChangedWaitTime);
        }
 
        /// <summary>
        /// 点击松开事件
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">E.</param>
        private void ChildrenUpEvent(object sender, MouseEventArgs e)
        {
            if (this.CanClick == false)
            {
                //不允许点击
                return;
            }
            if (sender is FrameLayoutStatuControl)
            {
                //LOG出力
                this.WriteLog();
            }
            try
            {
                //调用委托
                ButtonClickEvent?.Invoke(sender, e);
            }
            catch (Exception ex)
            {
                //出现未知错误
                var alert = new ShowMsgControl(ShowMsgType.Error, Language.StringByID(R.MyInternationalizationString.uUnKnownError));
                alert.Show();
                //Log出力
                HdlLogLogic.Current.WriteLog(ex);
            }
        }
 
        #endregion
 
        #region ■ 单击状态显示_______________________
 
        /// <summary>
        /// 设置单击后结束的状态
        /// </summary>
        public void SetClickNotSelectStatu()
        {
            this.IsSelectStatu = false;
            if (this.SelectStatuEvent != null)
            {
                this.SelectStatuEvent(false);
                return;
            }
 
            this.BackgroundColor = this.oldBackColor;
        }
 
        /// <summary>
        /// 设置单击时的状态
        /// </summary>
        public void SetClickSelectStatu()
        {
            this.IsSelectStatu = true;
            if (this.SelectStatuEvent != null)
            {
                this.SelectStatuEvent(true);
                return;
            }
 
            this.BackgroundColor = UserCenterColor.Current.RowSelectBackColor;
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 强制实施控件选中状态的特效
        /// </summary>
        /// <param name="waiTime"></param>
        public void StartSelectStatuAppeal(int waiTime)
        {
            if (this.IsSelectStatu == true)
            {
                return;
            }
            this.IsSelectStatu = true;
 
            //设置选择状态
            this.SetClickSelectStatu();
            HdlThreadLogic.Current.RunThread(() =>
            {
                System.Threading.Thread.Sleep(waiTime);
                HdlThreadLogic.Current.RunMain(() =>
                {
                    //设置不选择状态
                    this.SetClickNotSelectStatu();
                });
            });
        }
 
        /// <summary>
        /// 移除底层控件自身的单击事件
        /// </summary>
        public void RemoveBaseClickEvent()
        {
            this.MouseUpEventHandler -= ChildrenUpEvent;
            this.MouseDownEventHandler -= ChildrenDownEvent;
        }
 
        /// <summary>
        /// 控件摧毁
        /// </summary>
        public override void RemoveFromParent()
        {
            this.SelectStatuEvent = null;
 
            base.RemoveFromParent();
        }
 
        #endregion
 
        #region ■ Log出力____________________________
 
        /// <summary>
        /// Log出力
        /// </summary>
        private void WriteLog()
        {
            if (formName == null)
            {
                formName = string.Empty;
                View myView = this.Parent;
                for (; ; )
                {
                    if (myView == null)
                    {
                        break;
                    }
                    else if (myView is CommonFormBase)
                    {
                        formName = ((CommonFormBase)myView).FormID;
                        break;
                    }
                    myView = myView.Parent;
                }
            }
            HdlLogLogic.Current.WriteLog(1, formName + ".FrameLayoutControl 被点击");
        }
 
        #endregion
    }
}