using Shared;
using HDL_ON.UI.CSS;
using System;
using System.Text;
namespace HDL_ON.Stan
{
///
/// 显示一个信息框(请使用HdlMessageLogic调用)
///
public class ShowMsgControl
{
#region ■ 变量声明___________________________
///
/// 点击确认的事件
///
public Action ConfirmClickEvent = null;
///
/// 点击取消的事件
///
public Action CancelClickEvent = null;
///
/// 信息类型
///
private ShowMsgType msgType = ShowMsgType.Confirm;
///
/// 消息
///
private string msgText = string.Empty;
///
/// 确认按钮的文本
///
private string buttonOkText = null;
///
/// 取消按钮的文本
///
private string buttonCancelText = null;
///
/// 提示控件
///
private Tip myTip = null;
///
/// 等待时间
///
private int WaitTime = -1;
#endregion
#region ■ 初始化_____________________________
///
/// 显示一个需要确认的信息框
///
/// 信息类型
/// 信息
/// 确认按钮的文本
/// 取消按钮的文本
/// 等待时间,单位为秒,设置确认按钮在多长时间后才能够点击
public ShowMsgControl(ShowMsgType i_msgType, string i_msg, string i_buttonOkText = null, string i_buttonCancelText = null, int i_waitTime = -1)
{
//确认按钮文本
this.buttonOkText = i_buttonOkText == null ? Language.StringByID(StringId.Confirm) : i_buttonOkText;
this.buttonCancelText = i_buttonCancelText == null ? Language.StringByID(StringId.Cancel) : i_buttonCancelText;
this.msgType = i_msgType;
this.msgText = i_msg.Replace("{0}", "\r\n");
this.WaitTime = i_waitTime;
if (i_msgType == ShowMsgType.Tip)
{
myTip = new Tip();
myTip.Direction = AMPopTipDirection.None;
myTip.CloseTime = 2;
myTip.Text = i_msg;
}
}
#endregion
#region ■ 显示消息___________________________
///
/// 显示
///
public void Show()
{
try
{
if (myTip != null)
{
myTip.Show(MainPage.BasePageView);
myTip = null;
return;
}
if (this.msgType == ShowMsgType.TipRemind || this.msgType == ShowMsgType.TipSuccess)
{
//初始化控件
this.InitExTipControl();
}
else if (this.msgType == ShowMsgType.ConfirmSuccess || this.msgType == ShowMsgType.ConfirmFail)
{
//初始化控件
this.InitConfirmSuccessTypeContorl();
}
else
{
//初始化控件
this.InitMsgControl();
}
}
catch { }
}
#endregion
#region ■ 初始化控件_________________________
///
/// 初始化控件
///
private void InitMsgControl()
{
var dialogForm = new Dialog();
dialogForm.BackgroundColor = CSS_Color.DialogTransparentColor1;
//主控件
var frameMain = new NormalFrameLayout();
dialogForm.AddChidren(frameMain);
dialogForm.Show();
//计算用
var btnTemp = new ButtonCtrBase();
btnTemp.TextSize = CSS_FontSize.PromptFontSize_FirstLevel;
btnTemp.Text = msgText.Replace("\r\n", string.Empty);
//获取这个显示的内容的高度
int rowCount = btnTemp.GetRealRowCountByText();
//再看看它原来按换行符分割为几行
var myArry = msgText.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
//看看谁的行数多,就用谁的
if (myArry.Length > rowCount)
{
rowCount = myArry.Length;
}
int contentHeight = rowCount * Application.GetRealHeight(18);
if (rowCount <= 2)
{
//它有一个最小高度
contentHeight = Application.GetRealHeight(51);
}
//这个区域的高度比例为:显示内容到上部的距离(50)+显示内容的高度+显示内容到底部的距离(70)
//中间区域
var frameCenter = new NormalFrameLayout();
frameCenter.Gravity = Gravity.Center;
frameCenter.Width = Application.GetRealWidth(270);
frameCenter.Height = Application.GetRealHeight(50) + contentHeight + Application.GetRealHeight(70);
frameCenter.BackgroundColor = CSS_Color.MainBackgroundColor;
frameCenter.BorderColor = 0x00000000;
frameCenter.BorderWidth = 0;
frameCenter.Radius = (uint)Application.GetMinRealAverage(10);
frameMain.AddChidren(frameCenter);
//标题
var btnTitle = new NormalViewControl(frameCenter.Width - HdlControlResourse.XXLeft * 2, Application.GetRealHeight(22), false);
btnTitle.Y = Application.GetRealHeight(20);
btnTitle.Gravity = Gravity.CenterHorizontal;
btnTitle.TextColor = CSS_Color.MainColor;
btnTitle.TextSize = CSS_FontSize.SubheadingFontSize;
btnTitle.TextAlignment = TextAlignment.Center;
btnTitle.IsBold = true;
btnTitle.Text = Language.StringByID(StringId.Tip);
frameCenter.AddChidren(btnTitle);
//提示内容按钮
var btnMsg = new NormalViewControl(Application.GetRealWidth(258), contentHeight, false);
btnMsg.Y = btnTitle.Bottom + Application.GetRealHeight(8);
btnMsg.Gravity = Gravity.CenterHorizontal;
btnMsg.TextAlignment = TextAlignment.Center;
btnMsg.TextSize = CSS_FontSize.PromptFontSize_FirstLevel;
btnMsg.Text = msgText;
btnMsg.IsMoreLines = true;
frameCenter.AddChidren(btnMsg);
if (msgType == ShowMsgType.Confirm)
{
//初始化确认类型的底部按钮
this.InitBottomConfirmButton(dialogForm, frameCenter);
}
else
{
//初始化普通类型的底部按钮
this.InitBottomNormalButton(dialogForm, frameCenter);
}
}
///
/// 初始化确认类型的底部按钮
///
///
private void InitBottomConfirmButton(Dialog dialogForm, NormalFrameLayout frameCenter)
{
//取消
var btnCancel = new NormalViewControl(frameCenter.Width / 2, Application.GetRealHeight(43), false);
btnCancel.Gravity = Gravity.BottomLeft;
btnCancel.TextAlignment = TextAlignment.Center;
btnCancel.TextSize = CSS_FontSize.SubheadingFontSize;
btnCancel.Text = this.buttonCancelText;
//btnCancel.SetCornerWithSameRadius((uint)Application.GetMinRealAverage(10), HDLUtils.RectCornerBottomLeft);
frameCenter.AddChidren(btnCancel);
btnCancel.ButtonClickEvent += (sender, e) =>
{
//关闭界面
dialogForm.Close();
//回调函数
this.CancelClickEvent?.Invoke();
this.ConfirmClickEvent = null;
this.CancelClickEvent = null;
};
//线
var btnLine = new NormalViewControl(frameCenter.Width / 2, HdlControlResourse.BottomLineHeight, false);
btnLine.Y = btnCancel.Y - HdlControlResourse.BottomLineHeight;
btnLine.BackgroundColor = CSS_Color.DividingLineColor;
frameCenter.AddChidren(btnLine);
//确认
var btnConfirm = new NormalViewControl(frameCenter.Width - btnCancel.Width, Application.GetRealHeight(45), false);
btnConfirm.X = btnCancel.Right;
btnConfirm.Y = btnLine.Y;
btnConfirm.TextAlignment = TextAlignment.Center;
btnConfirm.TextSize = CSS_FontSize.SubheadingFontSize;
btnConfirm.TextColor = CSS_Color.MainBackgroundColor;
btnConfirm.BackgroundColor = CSS_Color.MainColor;
btnConfirm.Text = this.buttonOkText;
frameCenter.AddChidren(btnConfirm);
btnConfirm.SetCornerWithSameRadius((uint)Application.GetMinRealAverage(10), HDLUtils.RectCornerBottomRight);
btnConfirm.ButtonClickEvent += (sender, e) =>
{
//关闭界面
dialogForm.Close();
//回调函数
this.ConfirmClickEvent?.Invoke();
this.ConfirmClickEvent = null;
this.CancelClickEvent = null;
};
//开启等待时间
this.StartWaitTime(btnConfirm);
}
///
/// 初始化普通类型的底部按钮
///
///
///
private void InitBottomNormalButton(Dialog dialogForm, NormalFrameLayout frameCenter)
{
//确认
var btnConfirm = new NormalViewControl(frameCenter.Width, Application.GetRealHeight(45), false);
btnConfirm.Gravity = Gravity.BottomCenter;
btnConfirm.TextAlignment = TextAlignment.Center;
btnConfirm.TextSize = CSS_FontSize.SubheadingFontSize;
btnConfirm.TextColor = CSS_Color.MainBackgroundColor;
btnConfirm.BackgroundColor = CSS_Color.MainColor;
btnConfirm.Text = this.buttonOkText;
frameCenter.AddChidren(btnConfirm);
btnConfirm.SetCornerWithSameRadius((uint)Application.GetMinRealAverage(10), HDLUtils.RectCornerBottomLeft | HDLUtils.RectCornerBottomRight);
btnConfirm.ButtonClickEvent += (sender, e) =>
{
//关闭界面
dialogForm.Close();
//回调函数
this.ConfirmClickEvent?.Invoke();
this.ConfirmClickEvent = null;
this.CancelClickEvent = null;
};
//开启等待时间
this.StartWaitTime(btnConfirm);
}
///
/// 初始化特殊的tip控件,仅支持 TipRemind 和 TipSuccess
///
private void InitExTipControl()
{
//获取当前的界面
var nowActionForm = MainPage.BasePageView.GetChildren(MainPage.BasePageView.ChildrenCount - 1) as ViewGroup;
if (nowActionForm == null)
{
return;
}
//主控件
var frameMain = new NormalFrameLayout();
frameMain.Height = nowActionForm.Height;
frameMain.Width = nowActionForm.Width;
nowActionForm.AddChidren(frameMain);
frameMain.ButtonClickEvent += (sender, e) =>
{
frameMain.RemoveFromParent();
};
//计算用
var btnTemp = new ButtonCtrBase();
btnTemp.Text = msgText.Replace("\r\n", string.Empty);
//获取这个显示的内容的高度(蓝湖的黑色背景的宽度为198,然后再减去余白)
int rowCount = btnTemp.GetRealRowCountByText(Application.GetRealWidth(198) - HdlControlResourse.XXLeft * 2);
//再看看它原来按换行符分割为几行
var myArry = msgText.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
//看看谁的行数多,就用谁的
if (myArry.Length > rowCount)
{
rowCount = myArry.Length;
}
int contentHeight = rowCount * Application.GetRealHeight(20);
//蓝湖黑色背景的Y轴为302
var frameBlack = new FrameLayout();
//如果是多行时,减掉一行的高度,则得出新增加的高度,然后再除以2,则为Y轴原始占比
frameBlack.Y = Application.GetRealHeight(302) - (contentHeight - Application.GetRealHeight(20)) / 2;
frameBlack.Width = Application.GetRealWidth(198);
//实际计算出来的高度为 62+文本高度+16
frameBlack.Height = Application.GetRealHeight(62) + contentHeight + Application.GetRealHeight(16);
frameBlack.Radius = (uint)Application.GetRealWidth(12);
frameBlack.Gravity = Gravity.CenterHorizontal;
frameBlack.BackgroundColor = CSS_Color.DialogTransparentColor1;
frameMain.AddChidren(frameBlack);
//图标
var btnIcon = new IconViewControl(42);
btnIcon.Y = Application.GetRealHeight(10);
btnIcon.Gravity = Gravity.CenterHorizontal;
btnIcon.UnSelectedImagePath = this.msgType == ShowMsgType.TipSuccess ? "Public/MsgIcon/TipSuccessIcon.png" : "Public/MsgIcon/TipIconWhite.png";
frameBlack.AddChidren(btnIcon);
//显示文本
var btnText = new NormalViewControl(frameBlack.Width - HdlControlResourse.XXLeft * 2, contentHeight, false);
btnText.Y = btnIcon.Bottom + Application.GetRealHeight(10);
btnText.TextAlignment = TextAlignment.Center;
btnText.TextColor = CSS_Color.MainBackgroundColor;
btnText.Gravity = Gravity.CenterHorizontal;
btnText.IsMoreLines = true;
btnText.Text = this.msgText;
frameBlack.AddChidren(btnText);
//开启关闭时间
this.StartCloseTime(frameMain);
}
///
/// 初始化确认成功型控件,仅支持 ConfirmSuccess 和 ConfirmFail
///
private void InitConfirmSuccessTypeContorl()
{
var dialogForm = new Dialog();
dialogForm.BackgroundColor = CSS_Color.DialogTransparentColor1;
//主控件
var frameMain = new NormalFrameLayout();
dialogForm.AddChidren(frameMain);
dialogForm.Show();
//白色背景
var frameBack = new FrameLayout();
frameBack.Y = Application.GetRealHeight(223);
frameBack.Gravity = Gravity.CenterHorizontal;
frameBack.Width = Application.GetRealWidth(288);
frameBack.Height = Application.GetRealHeight(245);
frameBack.BackgroundColor = CSS_Color.MainBackgroundColor;
frameBack.Radius = (uint)Application.GetRealWidth(12);
frameMain.AddChidren(frameBack);
//大图标(蓝湖上,它高出白色区域70像素,因为高宽的比率是不一样的,所以需要计算一下比率)
decimal decProportion = 70m / 160;
var btnIcon = new IconViewControl(160);
btnIcon.Y = frameBack.Y - (int)(decProportion * btnIcon.IconSize);
btnIcon.Gravity = Gravity.CenterHorizontal;
btnIcon.UnSelectedImagePath = this.msgType == ShowMsgType.ConfirmSuccess ? "Public/Dialog/DialogTipTitleIcon_1.png" : "Public/Dialog/DialogTipTitleIcon_2.png";
frameMain.AddChidren(btnIcon);
//消息
var btnMsg = new NormalViewControl(frameMain.Width - HdlControlResourse.XXLeft * 2, Application.GetRealHeight(50), false);
//取大图标的下部高度,然后再加间距10
btnMsg.Y = btnIcon.Height - (frameBack.Y - btnIcon.Y) + Application.GetRealHeight(10);
btnMsg.Gravity = Gravity.CenterHorizontal;
btnMsg.TextAlignment = TextAlignment.TopCenter;
btnMsg.IsMoreLines = true;
btnMsg.TextSize = CSS_FontSize.SubheadingFontSize;
btnMsg.TextColor = CSS_Color.MainColor;
btnMsg.Text = this.msgText;
frameBack.AddChidren(btnMsg);
//确认按钮
var btnConfirm = new BottomClickButton(220);
btnConfirm.Text = this.buttonOkText;
btnConfirm.Y = Application.GetRealHeight(169);
frameBack.AddChidren(btnConfirm);
btnConfirm.ButtonClickEvent += (sender, e) =>
{
//关闭界面
dialogForm.Close();
//回调函数
this.ConfirmClickEvent?.Invoke();
this.ConfirmClickEvent = null;
this.CancelClickEvent = null;
};
//开启等待时间
this.StartWaitTime(btnConfirm);
}
#endregion
#region ■ 开启等待时间_______________________
///
/// 开启等待时间
///
/// 确认按钮
private void StartWaitTime(ButtonCtrBase btnConfirm)
{
if (this.WaitTime <= 0)
{
return;
}
btnConfirm.CanClick = false;
HdlThreadLogic.Current.RunThread(() =>
{
//显示剩余等待时间
while (btnConfirm.Parent != null && this.WaitTime >= 0)
{
HdlThreadLogic.Current.RunMain(() =>
{
btnConfirm.Text = this.buttonOkText + "(" + this.WaitTime + ")";
}, ShowErrorMode.NO);
System.Threading.Thread.Sleep(1000);
this.WaitTime--;
}
HdlThreadLogic.Current.RunMain(() =>
{
//可以点击
btnConfirm.Text = this.buttonOkText;
btnConfirm.CanClick = true;
}, ShowErrorMode.NO);
});
}
///
/// 开启关闭时间
///
/// 主界面
private void StartCloseTime(FrameLayout frameMain)
{
if (this.WaitTime <= 0)
{
return;
}
HdlThreadLogic.Current.RunThread(() =>
{
while (frameMain.Parent != null && this.WaitTime >= 0)
{
System.Threading.Thread.Sleep(1000);
this.WaitTime--;
}
HdlThreadLogic.Current.RunMain(() =>
{
frameMain?.RemoveFromParent();
}, ShowErrorMode.NO);
});
}
#endregion
}
}