using System;
using System.Threading.Tasks;
namespace Shared.Phone.UserCenter
{
///
/// 制作一个搜索控件(控件左边为文本输入,控件右边有一个搜索图标,外面是一个有弧度的框),
/// 添加控件后,请调用LoadEvent()函数进行回调函数的绑定
///
public class SearchEditText : FrameLayout
{
#region ■ 变量声明__________________________
///
/// 为了对应子线程
///
private string m_Text = string.Empty;
///
/// 文本值
///
/// The text.
public string Text
{
get
{
return m_Text;
}
set
{
this.m_Text = value;
Application.RunOnMainThread(() =>
{
txtSearch.Text = this.m_Text;
});
}
}
///
/// 搜索控件
///
private InputTextControl txtSearch = null;
///
/// 调用回调函数
///
private bool LoadAction = false;
///
/// 回调函数
///
private Action actionMethod = null;
#endregion
#region ■ 初始化____________________________
///
/// 制作一个搜索控件(控件左边为文本输入,控件右边有一个搜索图标,外面是一个有弧度的框),
/// 添加控件后,请调用BindEvent()函数进行回调函数的绑定
///
/// 宽度
/// 高度
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;
}
///
/// 绑定事件
///
/// 回调函数:值是输入的关键字
public void BindEvent(Action action)
{
this.LoadAction = true;
this.actionMethod = action;
//控件初始化
this.InitControl(action);
//打开搜索键值的线程
this.StartSearchKeysThead();
}
///
/// 移除事件
///
public void RemoveEvent()
{
this.LoadAction = false;
}
///
/// 控件初始化
///
/// 回调函数:值是输入的关键字
private void InitControl(Action 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 ■ 开启线程__________________________
///
/// 线程开启中
///
private bool isThreadAciton = false;
///
/// 能否执行搜索
///
private bool isCanSearch = false;
///
/// 下一个搜索键值
///
private string nextSearchKeys = string.Empty;
///
/// 打开搜索键值的线程
///
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();
}
///
/// 关闭线程
///
public void CloseThead()
{
this.isThreadAciton = false;
}
#endregion
}
}