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;
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;
}
//初始化控件
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, Application.GetRealHeight(22), false);
btnTitle.Y = Application.GetRealHeight(20);
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);
}
#endregion
#region ■ 开启等待时间_______________________
///
/// 开启等待时间(此函数只用于安卓)
///
/// 确认按钮
private void StartWaitTime(NormalViewControl 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 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);
});
}
#endregion
}
}