using System;
|
using System.Collections.Generic;
|
using System.Text;
|
|
namespace Shared.Phone.UserCenter
|
{
|
/// <summary>
|
/// Botton的最初原型(不建议修改和直接使用):标准字体和颜色,文字向左靠齐,14号字
|
/// </summary>
|
public class ButtonBase : Button
|
{
|
#region ■ 变量声明___________________________
|
|
/// <summary>
|
/// 设置能否触点击事件
|
/// </summary>
|
private bool m_CanClick = true;
|
/// <summary>
|
/// 设置能否触点击事件
|
/// </summary>
|
public bool CanClick
|
{
|
get { return m_CanClick; }
|
set
|
{
|
m_CanClick = value;
|
//能够点击,则显示没有点击过的状态
|
if (m_CanClick == true) { this.SetNotClickStatu(); }
|
//不能点击,则显示已经点击了的状态
|
else { this.SetClickStatu(); }
|
}
|
}
|
|
/// <summary>
|
/// 控件的点击事件(此事件被认可为执行按钮按下事件,受CanClick属性控制)
|
/// </summary>
|
public Action<Button, MouseEventArgs> ButtonClickEvent = null;
|
|
#endregion
|
|
#region ■ 初始化_____________________________
|
|
/// <summary>
|
/// Botton的最初原型(不建议修改和直接使用):标准字体和颜色,文字向左靠齐,14号字
|
/// </summary>
|
public ButtonBase()
|
{
|
//测试,全体按钮为14号字
|
this.TextSize = 14;
|
|
this.TextColor = UserCenterColor.Current.TextColor1;
|
this.TextAlignment = TextAlignment.CenterLeft;
|
|
//点击事件
|
this.MouseUpEventHandler += ButtonBase_MouseUpEventHandler;
|
}
|
|
/// <summary>
|
/// 初始化控件大小(不以平均值进行真实数值计算)
|
/// </summary>
|
/// <param name="i_Width">宽度</param>
|
/// <param name="i_Height">高度</param>
|
/// <param name="real">是否计算真实值</param>
|
public void InitSize(int i_Width, int i_Height, bool real = true)
|
{
|
if (real == true)
|
{
|
i_Width = Application.GetRealWidth(i_Width);
|
i_Height = Application.GetRealHeight(i_Height);
|
}
|
this.Height = i_Height;
|
this.Width = i_Width;
|
}
|
|
/// <summary>
|
/// 初始化控件大小(不以平均值进行真实数值计算)
|
/// </summary>
|
/// <param name="i_Width">宽度</param>
|
/// <param name="real">是否计算真实值</param>
|
public void InitSize(int i_Width, bool real = false)
|
{
|
if (real == true)
|
{
|
i_Width = Application.GetRealWidth(i_Width);
|
}
|
|
this.Height = ControlCommonResourse.NormalControlHeight;
|
this.Width = i_Width;
|
}
|
|
/// <summary>
|
/// 初始化控件大小(以平均值进行真实数值计算)
|
/// </summary>
|
/// <param name="i_Width">宽度</param>
|
/// <param name="i_Height">高度</param>
|
/// <param name="real">是否计算真实值</param>
|
public void InitAvgSize(int i_Width, int i_Height, bool real = true)
|
{
|
if (real == true)
|
{
|
i_Width = Application.GetMinRealAverage(i_Width);
|
i_Height = Application.GetMinRealAverage(i_Height);
|
}
|
|
this.Height = i_Height;
|
this.Width = i_Width;
|
}
|
|
#endregion
|
|
#region ■ 点击事件___________________________
|
|
/// <summary>
|
/// 点击事件
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void ButtonBase_MouseUpEventHandler(object sender, MouseEventArgs e)
|
{
|
if (ButtonClickEvent == null)
|
{
|
this.MouseUpEventHandler -= ButtonBase_MouseUpEventHandler;
|
return;
|
}
|
if (CanClick == true)
|
{
|
//Log出力
|
this.WriteLog();
|
|
try
|
{
|
this.ButtonClickEvent?.Invoke(this, e);
|
}
|
catch (Exception ex)
|
{
|
//出现未知错误,数据丢失
|
var alert = new ShowMsgControl(ShowMsgType.Error, Language.StringByID(R.MyInternationalizationString.uUnknownErrorAndDataLost));
|
alert.Show();
|
//Log出力
|
HdlLogLogic.Current.WriteLog(ex);
|
}
|
}
|
}
|
|
#endregion
|
|
#region ■ 设置点击状态_______________________
|
|
/// <summary>
|
/// 设置点击后的状态(此方法由各自控件进行重载)
|
/// </summary>
|
public virtual void SetClickStatu()
|
{
|
}
|
|
/// <summary>
|
/// 设置非点击后的状态(此方法由各自控件进行重载)
|
/// </summary>
|
public virtual void SetNotClickStatu()
|
{
|
}
|
|
#endregion
|
|
#region ■ 一般方法___________________________
|
|
/// <summary>
|
/// 重置单击事件(此操作委托会变null)
|
/// </summary>
|
public void ResetClickEvent()
|
{
|
ButtonClickEvent = null;
|
this.MouseUpEventHandler -= this.ButtonBase_MouseUpEventHandler;
|
this.MouseUpEventHandler += this.ButtonBase_MouseUpEventHandler;
|
}
|
|
/// <summary>
|
/// 控件摧毁
|
/// </summary>
|
public override void RemoveFromParent()
|
{
|
ButtonClickEvent = null;
|
base.RemoveFromParent();
|
}
|
|
/// <summary>
|
/// Y轴重置(真实数值,没有父容器无效)
|
/// </summary>
|
/// <param name="alignment">上下对齐方式</param>
|
/// <param name="Space">上下两部分的间距</param>
|
public void ReSetYaxis(UViewAlignment alignment, int Space = 0)
|
{
|
if (this.Parent == null)
|
{
|
return;
|
}
|
//Y轴重置
|
this.Y = UserCenterLogic.GetControlChidrenYaxis(this.Parent.Height, this.Height, alignment, Space);
|
}
|
|
/// <summary>
|
/// 根据文本,计算它实际的宽度(返回的是真实值)
|
/// </summary>
|
/// <param name="textSize">字体大小,省略时使用当前控件的字体大小</param>
|
/// <param name="i_text">需要计算的文本信息,省略时使用当前控件的文本</param>
|
/// <returns></returns>
|
public int GetRealWidthByText(float textSize, string i_text = null)
|
{
|
if (i_text == null)
|
{
|
i_text = this.Text;
|
}
|
if (textSize == -1)
|
{
|
textSize = this.TextSize;
|
}
|
|
int byteLength = 0;
|
for (int i = 0; i < i_text.Length; i++)
|
{
|
byteLength += Encoding.UTF8.GetBytes(i_text[i].ToString()).Length;
|
//int length = Encoding.UTF8.GetBytes(i_text[i].ToString()).Length;
|
//if (length == 1)
|
//{
|
// //英文
|
// byteLength += length;
|
// continue;
|
//}
|
////中文(暂时用中文对应)
|
//byteLength += Encoding.GetEncoding("gb2312").GetBytes(i_text[i].ToString()).Length;
|
}
|
int realWidth = byteLength * (int)textSize;
|
return Application.GetRealWidth(realWidth + 20);
|
}
|
|
/// <summary>
|
/// 添加底部阴影特效(确保拥有父控件后才调用)
|
/// </summary>
|
public void AddBottomShadow()
|
{
|
var btnShadow = new PicViewControl(this.Width, Application.GetMinRealAverage(45), false);
|
btnShadow.X = this.X;
|
btnShadow.Y = this.Bottom;
|
btnShadow.UnSelectedImagePath = "Item/BottomShadow.png";
|
this.Parent.AddChidren(btnShadow);
|
}
|
|
#endregion
|
|
#region ■ Log出力____________________________
|
|
/// <summary>
|
/// 该控件所属的界面名字
|
/// </summary>
|
private string formName = null;
|
/// <summary>
|
/// 控件名字
|
/// </summary>
|
private string controlName = null;
|
|
/// <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;
|
}
|
if (string.IsNullOrEmpty(this.Text) == false)
|
{
|
//这个控件的文本
|
controlName = this.Text;
|
}
|
else
|
{
|
//如果没有文本的话,它应该是一张图片
|
controlName = this.UnSelectedImagePath;
|
}
|
}
|
HdlLogLogic.Current.WriteLog(1, formName + "的[" + controlName + "]按键被点击");
|
}
|
|
#endregion
|
}
|
}
|