using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; namespace Shared.Phone.UserCenter { /// /// 界面最基层的底层共通 /// public class CommonFormBase : FrameLayout { #region ■ 变量声明___________________________ /// /// 画面的ID(自动编号,唯一主键,和各页面并无任何逻辑关系,但是它的用处大着呢) /// public string FormID = string.Empty; #endregion #region ■ 初始化_____________________________ /// /// 初始化界面框架 /// public virtual void InitForm() { //将当前的画面保存到内存当中 UserCenterLogic.AddActionForm(this); //Log出力 HdlLogLogic.Current.WriteLog(1, this.FormID + " 界面打开"); } /// /// 执行ShowForm()方法 /// /// 启动参数 public virtual void LoadShowFormMethod(params object[] parameter) { this.LoadFormMethodByName(this, "ShowForm", parameter); } #endregion #region ■ 添加界面___________________________ /// /// 添加画面,启动参数由指定画面的ShowForm函数所指定 /// /// 启动参数:参数由指定画面的ShowForm函数所指定 public virtual void AddForm(params object[] parameter) { return; } /// /// 添加指定画面,并移除当前画面,启动参数由指定画面的ShowForm函数所指定 /// /// 目标界面 /// 启动参数:参数由指定画面的ShowForm函数所指定 public void AddFromAndRemoveNowForm(CommonFormBase form, params object[] parameter) { //移除当前画面 this.CloseForm(); //添加画面 form.AddForm(parameter); } #endregion #region ■ 关闭界面___________________________ /// /// 画面关闭 /// public virtual void CloseForm() { //Log出力 HdlLogLogic.Current.WriteLog(1, this.FormID + " 界面关闭"); //移除接受在线状态推送 this.RemoveGatewayOnlinePush(); //移除设备属性变更推送 this.RemoveDeviceAttributePush(); //关闭进度条 this.CloseProgressBar(); //调用的是Base的移除控件函数 //而不是调用this的移除控件函 base.RemoveFromParent(); //从列表中移除(防止画面二重添加) UserCenterLogic.RemoveActionForm(this); } /// /// 画面关闭(在画面里面,请不要调用此方法,请使用CloseForm()方法) /// public override void RemoveFromParent() { //画面右滑时,关闭画面,调用自定义的CloseForm()方法 this.CloseForm(); } #endregion #region ■ 网关在线状态推送___________________ /// /// 设置网关接受在线状态推送 /// public void AddGatewayOnlinePush() { for (int i = 0; i < UserCenterResourse.listGatewayOnlinePushForm.Count; i++) { if (this.FormID == UserCenterResourse.listGatewayOnlinePushForm[i].FormID) { //已经存在,则不再添加 return; } } UserCenterResourse.listGatewayOnlinePushForm.Add(this); } /// /// 移除网关接受在线状态推送 /// private void RemoveGatewayOnlinePush() { UserCenterResourse.listGatewayOnlinePushForm.RemoveAll((obj) => { return obj.FormID == this.FormID; }); } /// /// 网关在线状态变更推送(只有在变更的时候才会推送) /// /// 网关对象 /// 在线状态变更后的状态 public virtual void GatewayOnlinePush(ZigBee.Device.ZbGateway gateWay, bool online) { } #endregion #region ■ 设备属性变更推送___________________ /// /// 设置接受设备属性变更推送 /// public void AddDeviceAttributePush() { for (int i = 0; i < UserCenterResourse.listDeviceAttributePushForm.Count; i++) { if (this.FormID == UserCenterResourse.listDeviceAttributePushForm[i].FormID) { //已经存在,则不再添加 return; } } UserCenterResourse.listDeviceAttributePushForm.Add(this); } /// /// 移除设备属性变更推送 /// private void RemoveDeviceAttributePush() { UserCenterResourse.listDeviceAttributePushForm.RemoveAll((obj) => { return obj.FormID == this.FormID; }); } /// /// 设备属性变更推送(只有在变更的时候才会推送) /// /// 设备对象 public virtual void DeviceAttributePush(ZigBee.Device.CommonDevice device) { } #endregion #region ■ 圆形进度条_________________________ /// /// 进度条启动 /// /// 初始文本 public void ShowProgressBar(string text = "") { if (this.FormID != UserCenterResourse.NowActionFormID) { return; } ProgressBar.Show(text); } /// /// 设置进度条的信息值(会自动计算百分比,值累加模式) /// /// 值 public void SetProgressValue(decimal value) { ProgressBar.SetValue(value); } /// /// 设置进度条的信息值 /// /// 值 public void SetProgressValue(string value) { ProgressBar.SetValue(value); } /// /// 设置进度条的信息值 /// /// 值 public void SetProgressMax(decimal value) { if (value == 0) { value = 100; } ProgressBar.SetMaxValue(value); } /// /// 关闭进度条 /// /// 是否显示重新加载的界面 public void CloseProgressBar(ShowReLoadMode mode = ShowReLoadMode.NO) { ProgressBar.Close(); if (mode == ShowReLoadMode.YES) { //显示重新加载的界面(主要是用在界面加载错误时,再次加载) this.ShowReLoadView(); } } /// /// 显示重新加载的界面(主要是用在界面加载错误时,再次加载) /// public virtual void ShowReLoadView() { } #endregion #region ■ 一般的方法_________________________ /// /// 显示信息框 /// /// 信息类型 /// 信息 /// 单击确认后执行的回调函数 /// 按钮的文本 public void ShowMassage(ShowMsgType msgType, string msg, Action action = null, string buttonText = null) { //空对象时,不显示 if (string.IsNullOrEmpty(msg)) { return; } Application.RunOnMainThread(() => { var alert = new ShowMsgControl(msgType, msg, buttonText); if (action != null) { alert.ConfirmClickEvent += () => { try { //回调函数 action?.Invoke(); } catch (Exception ex) { //出现未知错误,数据丢失 this.ShowMassage(ShowMsgType.Error, Language.StringByID(R.MyInternationalizationString.uUnknownErrorAndDataLost)); //Log出力 HdlLogLogic.Current.WriteLog(ex); } action = null; }; } alert.Show(); }); } /// /// 判断当前正在活动的界面是否是当前这个界面 /// /// public bool NowFormIsAction() { return UserCenterResourse.NowActionFormID == this.FormID; } #endregion #region ■ 反射方法___________________________ /// /// 关闭指定的画面 /// /// 指定要关闭的画面英文名字 public void CloseFormByFormName(string formName) { if (UserCenterResourse.DicActionForm.ContainsKey(formName) == false) { return; } //关闭指定画面 UserCenterResourse.DicActionForm[formName]?.CloseForm(); } /// /// 执行指定画面的方法 /// /// 指定画面的英文名 /// 指定要加载的方法名 /// 启动参数 public object LoadFormMethodByName(string formName, string method, params object[] parameter) { if (UserCenterResourse.DicActionForm.ContainsKey(formName) == false) { return null; } var form = UserCenterResourse.DicActionForm[formName]; return this.LoadFormMethodByName(form, method, parameter); } /// /// 执行指定画面的方法(注意:这个是专门调用异步,并且等待异步完成的高科技函数,不调用异步的情况,别使用此函数) /// /// 指定画面的英文名 /// 指定要加载的方法名 /// 启动参数 public async Task LoadFormMethodByNameAsync(string formName, string method, params object[] parameter) { if (UserCenterResourse.DicActionForm.ContainsKey(formName) == false) { return null; } var form = UserCenterResourse.DicActionForm[formName]; var task = this.LoadFormMethodByName(form, method, parameter) as Task; await task; var result = task.GetType().GetProperty("Result").GetValue(task, null); return result; } /// /// 执行指定画面的方法 /// /// 指定画面的英文名 /// 指定要加载的方法名 /// 启动参数 public object LoadFormMethodByName(CommonFormBase form, string method, params object[] parameter) { try { if (form == null) { return null; } return form.GetType().InvokeMember(method, System.Reflection.BindingFlags.InvokeMethod, null, form, parameter); } catch (Exception ex) { //出现未知错误,数据丢失 this.ShowMassage(ShowMsgType.Error, Language.StringByID(R.MyInternationalizationString.uUnknownErrorAndDataLost)); //Log出力 HdlLogLogic.Current.WriteLog(ex); return null; } } /// /// 使用反射方法,打开指定的画面(只支持继承于UserCenterCommonForm的画面) /// /// 画面的命名空间+画面的英文名 /// 启动参数 public void LoadFormByFullName(string fullName, params object[] parameter) { System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly(); var form = (CommonFormBase)assembly.CreateInstance(fullName); form.AddForm(parameter); } #endregion } }