using System;
using System.Collections.Generic;
using System.Text;
namespace Shared.Phone
{
///
/// 做成一个点击后能够显示点击状态的控件(基层控件)
///
public class FrameLayoutStatuControl : FrameLayoutBase
{
#region ■ 变量声明___________________________
///
/// 状态设置的事件(会重载底层效果)
///
public Action SelectStatuEvent;
///
/// 是否启用点亮功能(默认启用)
///
public bool UseClickStatu = true;
///
/// 原来的背景色
///
private uint oldBackColor = 0;
///
/// 当前是否已经处于选择状态
///
private bool IsSelectStatu = false;
///
/// 子控件Y轴偏移量(共通定义而已)
///
public int chidrenYaxis = 0;
///
/// 圆角度
///
public int RadiusEx
{
set { this.Radius = (uint)Application.GetRealHeight(value); }
}
#endregion
#region ■ 初始化_____________________________
///
/// 做成一个点击后能够显示点击状态的控件
///
/// 子控件Y轴偏移量(【列表控件的rowSpace/2】即可,不懂默认为0即可)
public FrameLayoutStatuControl(int i_ChidrenYaxis = 0)
{
this.chidrenYaxis = i_ChidrenYaxis;
//置空底层的事件
this.MouseUpEventHandler = null;
this.MouseUpEventHandler += ChildrenUpEvent;
this.MouseDownEventHandler += ChildrenDownEvent;
}
#endregion
#region ■ 绑定事件___________________________
///
/// 变更子控件的绑定模式
///
/// 子控件
/// 变更的绑定模式
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);
}
}
///
/// 绑定子控件事件(如果是复合控件,在初始化完成后,调用ChangedChidrenBindMode)
///
///
///
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 ■ 添加子控件_________________________
///
/// 添加子控件
///
/// 子控件
/// 绑定模式
public void AddChidren(View view, ChidrenBindMode chidrenBindMode = ChidrenBindMode.BindEvent)
{
this.oldBackColor = this.BackgroundColor;
base.AddChidren(view);
//绑定子控件事件
this.BindChidrenEvent(view, chidrenBindMode);
}
///
/// 添加子控件
///
///
public override void AddChidren(View view)
{
this.AddChidren(view, ChidrenBindMode.NotBind);
}
#endregion
#region ■ 控件事件___________________________
///
/// 点击按下事件(点亮)
///
/// Sender.
/// E.
private void ChildrenDownEvent(object sender, MouseEventArgs e)
{
if (this.UseClickStatu == false || this.CanClick == false)
{
return;
}
this.StartSelectStatuAppeal(HdlControlResourse.StatuChangedWaitTime);
}
///
/// 点击松开事件
///
/// Sender.
/// E.
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 ■ 单击状态显示_______________________
///
/// 设置单击后结束的状态
///
public void SetClickNotSelectStatu()
{
this.IsSelectStatu = false;
if (this.SelectStatuEvent != null)
{
this.SelectStatuEvent(false);
return;
}
this.BackgroundColor = this.oldBackColor;
}
///
/// 设置单击时的状态
///
public void SetClickSelectStatu()
{
this.IsSelectStatu = true;
if (this.SelectStatuEvent != null)
{
this.SelectStatuEvent(true);
return;
}
this.BackgroundColor = UserCenterColor.Current.RowSelectBackColor;
}
#endregion
#region ■ 一般方法___________________________
///
/// 强制实施控件选中状态的特效
///
///
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();
});
});
}
///
/// 移除底层控件自身的单击事件
///
public void RemoveBaseClickEvent()
{
this.MouseUpEventHandler -= ChildrenUpEvent;
this.MouseDownEventHandler -= ChildrenDownEvent;
}
///
/// 控件摧毁
///
public override void RemoveFromParent()
{
this.SelectStatuEvent = null;
base.RemoveFromParent();
}
#endregion
#region ■ Log出力____________________________
///
/// Log出力
///
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
}
}