xm
2019-07-16 b910cb79c9b5bcc204022a3cf9e6950f0a64dfbd
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
using System;
using System.Threading.Tasks;
 
namespace Shared.Phone.UserCenter
{
    /// <summary>
    /// 制作一个搜索控件(控件左边为文本输入,控件右边有一个搜索图标,外面是一个有弧度的框),
    /// 添加控件后,请调用LoadEvent()函数进行回调函数的绑定
    /// </summary>
    public class SearchEditText : FrameLayout
    {
        #region ■ 变量声明__________________________
        /// <summary>
        /// 为了对应子线程
        /// </summary>
        private string m_Text = string.Empty;
        /// <summary>
        /// 文本值
        /// </summary>
        /// <value>The text.</value>
        public string Text
        {
            get
            {
                return m_Text;
            }
            set
            {
                this.m_Text = value;
                Application.RunOnMainThread(() =>
                {
                    txtSearch.Text = this.m_Text;
                });
            }
        }
        /// <summary>
        /// 搜索控件
        /// </summary>
        private InputTextControl txtSearch = null;
        /// <summary>
        /// 调用回调函数
        /// </summary>
        private bool LoadAction = false;
        /// <summary>
        /// 回调函数
        /// </summary>
        private Action<string> actionMethod = null;
 
        #endregion
 
        #region ■ 初始化____________________________
        /// <summary>
        /// 制作一个搜索控件(控件左边为文本输入,控件右边有一个搜索图标,外面是一个有弧度的框),
        /// 添加控件后,请调用BindEvent()函数进行回调函数的绑定
        /// </summary>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        public SearchEditText(int width = 850, int height = 120)
        {
            //桌布控件
            this.Y = Application.GetRealHeight(45);
            this.Width = Application.GetRealWidth(width);
            this.Height = Application.GetRealHeight(height);
            this.Radius = 8;
            this.BorderWidth = 1;
            this.BorderColor = UserCenterColor.Current.TextFrameColor;
            this.Gravity = Gravity.CenterHorizontal;
        }
 
        /// <summary>
        /// 绑定事件
        /// </summary>
        /// <param name="action">回调函数:值是输入的关键字</param>
        public void BindEvent(Action<string> action)
        {
            this.LoadAction = true;
            this.actionMethod = action;
 
            //控件初始化
            this.InitControl(action);
 
            //打开搜索键值的线程
            this.StartSearchKeysThead();
        }
 
        /// <summary>
        /// 移除事件
        /// </summary>
        public void RemoveEvent()
        {
            this.LoadAction = false;
        }
 
        /// <summary>
        /// 控件初始化
        /// </summary>
        /// <param name="action">回调函数:值是输入的关键字</param>
        private void InitControl(Action<string> action)
        {
            if (txtSearch != null)
            {
                return;
            }
            //搜索
            txtSearch = new InputTextControl(this.Width - Application.GetRealWidth(150), this.Height)
            {
                X = Application.GetRealWidth(35),
                TextAlignment = TextAlignment.Center,
                Gravity = Gravity.CenterVertical,
                PlaceholderText = Language.StringByID(R.MyInternationalizationString.uSearch),
                PlaceholderTextColor = UserCenterColor.Current.Gray
            };
            this.AddChidren(txtSearch);
 
            var btnIcon = new IconViewControl(72)
            {
                Gravity = Gravity.CenterVertical,
                X = txtSearch.Right + Application.GetRealWidth(10),
                UnSelectedImagePath = "Item/Search.png",
                SelectedImagePath = "Item/SearchCancel.png"
            };
            btnIcon.MouseUpEventHandler += (sender, e) =>
            {
                txtSearch.Text = string.Empty;
            };
            this.AddChidren(btnIcon);
 
            string oldText = string.Empty;
            //值改变事件
            txtSearch.TextChangeEventHandler += (sender, e) =>
            {
                if (this.LoadAction == false)
                {
                    return;
                }
                this.nextSearchKeys = txtSearch.Text.Trim();
                this.m_Text = this.nextSearchKeys;
 
                if (oldText == this.nextSearchKeys)
                {
                    //输入的值一样
                    return;
                }
                oldText = this.nextSearchKeys;
                //更改图标
                if (this.nextSearchKeys  == string.Empty && btnIcon.IsSelected == true)
                {
                    btnIcon.IsSelected = false;
                }
                if (this.nextSearchKeys  != string.Empty && btnIcon.IsSelected == false)
                {
                    btnIcon.IsSelected = true;
                }
                //允许执行回调函数
                this.isCanSearch = true;
            };
        }
        #endregion
 
        #region ■ 开启线程__________________________
 
        /// <summary>
        /// 线程开启中
        /// </summary>
        private bool isThreadAciton = false;
        /// <summary>
        /// 能否执行搜索
        /// </summary>
        private bool isCanSearch = false;
        /// <summary>
        /// 下一个搜索键值
        /// </summary>
        private string nextSearchKeys = string.Empty;
        /// <summary>
        /// 打开搜索键值的线程
        /// </summary>
        private void StartSearchKeysThead()
        {
            if (isThreadAciton == true)
            {
                return;
            }
            this.isThreadAciton = true;
            new System.Threading.Thread(() =>
            {
                while (isThreadAciton && this.Parent != null)
                {
                    //根据逻辑,不能够搜索的话,等待
                    if (this.isCanSearch == false)
                    {
                        System.Threading.Thread.Sleep(1000);
                        continue;
                    }
                    //获取当前瞬间的键值
                    string nowSearchKeys = nextSearchKeys;
 
                    //根据搜索键值,调用回调函数
                    this.actionMethod(nowSearchKeys);
 
                    //处理完毕后,如果当前的检索键值与下一个检索键值一样的话
                    //说明用户已经不再输入键值了,这个时候,停止搜索
                    if (nowSearchKeys == nextSearchKeys)
                    {
                        this.isCanSearch = false;
                    }
 
                    //如果当前的检索键值与下一个检索键值不一样的话
                    //那就说明,用户再次输入了键值,这个时候,让线程继续跑下去
                }
            })
            { IsBackground = true }.Start();
        }
 
        /// <summary>
        /// 关闭线程
        /// </summary>
        public void CloseThead()
        {
            this.isThreadAciton = false;
        }
        #endregion
    }
}