HDL Home App 第二版本 旧平台金堂用 正在使用
chenqiyang
2022-06-22 dc0309e64f02227d8e1468b7326c07955f804612
ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Controls/CompoundControls/SearchEditText.cs
New file
@@ -0,0 +1,196 @@
using System;
using System.Threading.Tasks;
namespace Shared.Phone.UserCenter
{
    /// <summary>
    /// 制作一个根据键值进行搜索的控件
    /// 添加控件后,请调用LoadEvent()函数进行回调函数的绑定
    /// </summary>
    public class SearchTextControl : 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;
                txtSearch.Text = this.m_Text;
            }
        }
        /// <summary>
        /// 搜索控件
        /// </summary>
        private TextInputControl txtSearch = null;
        /// <summary>
        /// 回调函数
        /// </summary>
        private Action<string> actionMethod = null;
        /// <summary>
        /// 没有输入键值的显示文本
        /// </summary>
        private string tipText = string.Empty;
        #endregion
        #region ■ 初始化____________________________
        /// <summary>
        /// 制作一个根据键值进行搜索的控件
        /// 添加控件到父控件后,请调用BindEvent()函数进行回调函数的绑定
        /// </summary>
        /// <param name="i_tipText">没有输入键值的显示文本</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        public SearchTextControl(string i_tipText, int width = 965, int height = 104)
        {
            this.tipText = i_tipText;
            //桌布控件
            this.Width = Application.GetRealWidth(width);
            this.Height = Application.GetRealHeight(height);
            this.Gravity = Gravity.CenterHorizontal;
            this.Radius = (uint)Application.GetRealHeight(17);
            this.BackgroundColor = 0xfff8f7f7;
        }
        /// <summary>
        /// 控件初始化
        /// </summary>
        /// <param name="action">回调函数:值是输入的关键字</param>
        private void InitControl(Action<string> action)
        {
            //搜索图标
            var btnIcon = new IconViewControl(52);
            btnIcon.Gravity = Gravity.CenterVertical;
            btnIcon.X = ControlCommonResourse.XXLeft;
            btnIcon.UnSelectedImagePath = "Item/Search.png";
            this.AddChidren(btnIcon);
            //取消
            var btnCancel = new IconViewControl(58);
            btnCancel.X = this.Width - ControlCommonResourse.XXLeft / 2 - btnCancel.IconSize;
            btnCancel.UnSelectedImagePath = "Item/CancelIcon.png";
            btnCancel.Gravity = Gravity.CenterVertical;
            this.AddChidren(btnCancel);
            btnCancel.ButtonClickEvent += (sender, e) =>
            {
                this.Text = string.Empty;
            };
            //输入文本
            int textWidth = btnCancel.X - Application.GetRealWidth(132 + 10);
            txtSearch = new TextInputControl(textWidth, Application.GetRealHeight(49), false);
            txtSearch.X = Application.GetRealWidth(132);
            txtSearch.PlaceholderText = this.tipText;
            txtSearch.Gravity = Gravity.CenterVertical;
            txtSearch.TextSize = 12;
            txtSearch.PlaceholderTextColor = UserCenterColor.Current.TextGrayColor1;
            this.AddChidren(txtSearch);
            string oldText = string.Empty;
            //值改变事件
            txtSearch.TextChangeEventHandler += (sender, e) =>
            {
                this.nextSearchKeys = txtSearch.Text.Trim();
                this.m_Text = this.nextSearchKeys;
                if (oldText == this.nextSearchKeys)
                {
                    //输入的值一样
                    return;
                }
                oldText = this.nextSearchKeys;
                //允许执行回调函数
                this.isCanSearch = true;
            };
        }
        #endregion
        #region ■ 绑定事件__________________________
        /// <summary>
        /// 绑定事件
        /// </summary>
        /// <param name="action">回调函数:值是输入的关键字</param>
        public void BindEvent(Action<string> action)
        {
            this.actionMethod = action;
            //控件初始化
            this.InitControl(action);
            //打开搜索键值的线程
            this.StartSearchKeysThead();
        }
        #endregion
        #region ■ 开启线程__________________________
        /// <summary>
        /// 能否执行搜索
        /// </summary>
        private bool isCanSearch = false;
        /// <summary>
        /// 下一个搜索键值
        /// </summary>
        private string nextSearchKeys = string.Empty;
        /// <summary>
        /// 打开搜索键值的线程
        /// </summary>
        private void StartSearchKeysThead()
        {
            HdlThreadLogic.Current.RunThread(() =>
            {
                while (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;
                    }
                    //如果当前的检索键值与下一个检索键值不一样的话
                    //那就说明,用户再次输入了键值,这个时候,让线程继续跑下去
                }
            });
        }
        #endregion
        #region ■ 控件摧毁__________________________
        /// <summary>
        /// 控件摧毁
        /// </summary>
        public override void RemoveFromParent()
        {
            actionMethod = null;
            base.RemoveFromParent();
        }
        #endregion
    }
}