using System; using System.Collections.Generic; using System.Threading.Tasks; namespace Shared.Phone.UserCenter { /// /// 个人中心的共通基层画面,继承于此共通的画面,请使用AddForm()函数, /// 或者AddFromAndRemoveNowForm()函数实现添加画面,然后在各自的画面中, /// 实现一个ShowForm()的函数(参数由添加画面时,使用的函数的参数指定) /// public class UserCenterCommonForm : FrameLayout { #region ■ 变量声明___________________________ /// /// TopMenuFrameLayout /// public FrameLayout topMenuFrameLayout = null; /// /// TopFrameLayout /// public FrameLayout topFrameLayout = null; /// /// bodyFrameLayout /// public FrameLayout bodyFrameLayout = null; /// /// 画面的ID(自动编号,唯一主键,和各页面并无任何逻辑关系,但是它的用处大着呢) /// public string FormID = string.Empty; /// /// 缓存启动参数 /// private object[] m_parameter = null; #endregion #region ■ 可重载的方法_______________________ /// /// 初始化界面框架 /// public void InitForm() { //将当前的画面保存到内存当中 UserCenterLogic.AddActionForm(this); //初始化头部控件 this.InitTopFrameLayout(); //初始化中部控件 this.InitBodyFrameLayout(); } /// /// 执行ShowForm()方法 /// /// 启动参数 public void LoadShowFormMethod(params object[] parameter) { this.m_parameter = parameter; this.LoadFormMethodByName(this, "ShowForm", parameter); } /// /// 画面关闭 /// /// 是否关闭界面,false的时候,只会调用关闭函数里面的附加功能 public virtual void CloseForm(bool isCloseForm = true) { //移除接受在线状态推送 this.RemoveGatewayOnlinePush(); if (isCloseForm == false) { //不关闭界面 return; } this.bodyFrameLayout.RemoveAll(); //从列表中移除(防止画面二重添加) UserCenterLogic.RemoveActionForm(this); //调用的是Base的移除控件函数 //而不是调用this的移除控件函 base.RemoveFromParent(); } /// /// 画面关闭(在画面里面,请不要调用此方法,请使用CloseForm()方法) /// public override void RemoveFromParent() { //画面右滑时,关闭画面,调用自定义的CloseForm()方法 this.CloseForm(); } #endregion #region ■ 添加界面___________________________ /// /// 添加画面,启动参数由指定画面的ShowForm函数所指定 /// /// 对象画面 /// 启动参数:参数由指定画面的ShowForm函数所指定 public void AddForm(UserCenterCommonForm newform, params object[] parameter) { //检测能否追加画面(防止画面二重添加),当点击过快时,会有几率二重添加 if (UserCenterLogic.CheckCanAddForm(newform) == false) { return; } UserView.HomePage.Instance.AddChidren(newform); UserView.HomePage.Instance.PageIndex += 1; //初始化界面框架 newform.InitForm(); //执行ShowForm()方法 newform.LoadShowFormMethod(parameter); } /// /// 添加指定画面,并移除当前画面,启动参数由指定画面的ShowForm函数所指定 /// /// 对象画面 /// 启动参数:参数由指定画面的ShowForm函数所指定 public void AddFromAndRemoveNowForm(UserCenterCommonForm newform, params object[] parameter) { //移除当前画面 this.CloseForm(); //添加画面 this.AddForm(newform, parameter); } #endregion #region ■ 初始化界面_________________________ #region ■ Top________________________________ /// /// 初始化头部控件 /// public void InitTopFrameLayout() { //初始化头部FrameLayout this.InitTopFrameLayout(this, ref topFrameLayout); //返回键 BackViewControl back = (BackViewControl)topFrameLayout.GetTagByKey("btnBack"); back.MouseUpEventHandler += (sender, e) => { //this.Animate = Animate.LeftToRight; //画面关闭 this.CloseForm(); }; } /// /// Top标准化 /// private void InitTopFrameLayout(FrameLayout FormframeLayout, ref FrameLayout TopframeLayout) { //头部做成 MakeTopLayout(FormframeLayout, ref TopframeLayout); } /// /// 头部做成 /// /// Form. /// Topframe. private void MakeTopLayout(FrameLayout form, ref FrameLayout topframe) { if (topframe != null) { topframe.RemoveAll(); } //TopMenuFrameLayout做成 topMenuFrameLayout = this.MakeTopMenuLayout(); topMenuFrameLayout.Name = "topMenuFrameLayout"; form.AddChidren(topMenuFrameLayout); //TopFrameLayout做成 topframe = this.MakeTopLayout(); topframe.Y = topMenuFrameLayout.Bottom; topframe.Name = "topFrameLayout"; form.AddChidren(topframe); //返回键 BackViewControl btnBack = this.MakeBackButton(topframe); //标题 this.MakeTitleButton(topframe); } /// /// TopFrameLayout做成 /// /// The top layout. private FrameLayout MakeTopLayout() { FrameLayout topframeLayout = new FrameLayout(); topframeLayout.Width = LayoutParams.MatchParent; topframeLayout.Height = ControlCommonResourse.TopFrameHeight; topframeLayout.BackgroundColor = UserCenterColor.Current.TopFrameLayout; return topframeLayout; } /// /// TopMenuFrameLayout做成 /// /// The top layout. private FrameLayout MakeTopMenuLayout() { FrameLayout topMenuframeLayout = new FrameLayout(); topMenuframeLayout.Width = LayoutParams.MatchParent; topMenuframeLayout.Height = ControlCommonResourse.TopMenuFrameHeight; topMenuframeLayout.BackgroundColor = UserCenterColor.Current.TopFrameLayout; return topMenuframeLayout; } /// /// 返回键做成 /// /// The back button. private BackViewControl MakeBackButton(FrameLayout topframeLayout) { var btnBack = new BackViewControl(); topframeLayout.AddChidren(btnBack); topframeLayout.AddTag("btnBack", btnBack); return btnBack; } /// /// 标题控件做成 /// /// The title button. private TopLayoutTitleView MakeTitleButton(FrameLayout topframeLayout) { var txttitle = new TopLayoutTitleView(); topframeLayout.AddChidren(txttitle); topframeLayout.AddTag("txtTitle", txttitle); return txttitle; } #endregion #region ■ Middle_____________________________ /// /// 初始化中部控件 /// public void InitBodyFrameLayout() { //初始化中部FrameLayout this.InitBodyFrameLayout(this, ref bodyFrameLayout, topFrameLayout.Bottom); } /// /// Middle标准化 /// private void InitBodyFrameLayout(FrameLayout FormframeLayout, ref FrameLayout MidframeLayout, int YY) { if (MidframeLayout != null) { MidframeLayout.RemoveAll(); } //中部FrameLayout做成 MidframeLayout = this.MakeMiddleLayout(YY); MidframeLayout.Name = "bodyFrameLayout"; FormframeLayout.AddChidren(MidframeLayout); } /// /// 中部FrameLayout做成 /// /// The middle layout. /// Yy. private FrameLayout MakeMiddleLayout(int YY) { FrameLayout MidframeLayout = new FrameLayout(); MidframeLayout.Width = LayoutParams.MatchParent; MidframeLayout.Height = ControlCommonResourse.BodyFrameHeight; MidframeLayout.Y = YY; MidframeLayout.BackgroundColor = UserCenterColor.Current.BodyFrameLayout; return MidframeLayout; } #endregion #endregion #region ■ 网关在线状态推送___________________ /// /// 设置网关接受在线状态推送 /// public void AddGatewayOnlinePush() { UserCenterResourse.listGatewayOnlinePushForm.Add(this); } /// /// 移除网关接受在线状态推送 /// public 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 ShowProgressBar(string text = "") { 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(); } } #endregion #region ■ 一般的方法_________________________ /// /// 设置标题信息 /// /// Title. public void SetTitleText(string title) { //设置头部信息 Button btntitle = (Button)topFrameLayout.GetTagByKey("txtTitle"); btntitle.Text = title; } /// /// 移除返回键 /// public void RemoveBackButton() { //移除返回键 Button back = (Button)topFrameLayout.GetTagByKey("btnBack"); topFrameLayout.Remove(back); } /// /// 显示错误信息(只针对错误信息) /// /// 信息 /// 单击确认后的函数(请确认这是一个共有方法) /// 回调函数的启动参数 public void ShowErrorMsg(string msg, string methodName = null, params object[] obj) { //空对象时,不显示 if (string.IsNullOrEmpty(msg)) { return; } Application.RunOnMainThread(() => { var alert = new ErrorMsgControl(msg); alert.Show(); if (string.IsNullOrEmpty(methodName) == false) { alert.ResultEventHandler += (sender, result) => { this.LoadFormMethodByName(this, methodName, obj); }; } }); } /// /// 显示普通信息(只针对普通信息) /// /// 信息 /// 单击确认后的函数(请确认这是一个共有方法) /// 回调函数的启动参数 public void ShowNormalMsg(string msg, string methodName = null, params object[] obj) { //空对象时,不显示 if (string.IsNullOrEmpty(msg)) { return; } Application.RunOnMainThread(() => { var alert = new NormalMsgControl(msg); alert.Show(); if (methodName != null) { alert.ResultEventHandler += (sender, result) => { this.LoadFormMethodByName(this, methodName, obj); }; } }); } /// /// 显示一个需要确认的信息框 /// /// 信息 /// 方法名(请确认这是一个共有方法) /// 回调函数的启动参数 public void ShowConfirmMsg(string msg, string methodName = null, params object[] obj) { Application.RunOnMainThread(() => { var alert = new ConfirmMsgControl(msg); alert.Show(); if (methodName != null) { alert.ResultEventHandler += (sender, result) => { if (result == true) { this.LoadFormMethodByName(this, methodName, obj); } }; } }); } /// /// 显示Tip信息框 /// /// Text. /// Close time. public void ShowTip(string text, int closeTime = 2) { Application.RunOnMainThread(() => { var tip = new TipViewControl(text, closeTime); tip.ShowView(); }); } /// /// 判断当前正在活动的界面是否是当前这个界面 /// /// 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; } UserCenterCommonForm 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(UserCenterCommonForm form, string method, params object[] parameter) { try { return form.GetType().InvokeMember(method, System.Reflection.BindingFlags.InvokeMethod, null, form, parameter); } catch (Exception ex) { string msg = ex.Message + "\r\n"; msg += ex.TargetSite.ToString(); this.ShowErrorMsg(msg); return null; } } /// /// 使用反射方法,打开指定的画面(只支持继承于UserCenterCommonForm的画面) /// /// 画面的命名空间+画面的英文名 /// 启动参数 public void LoadFormByFullName(string fullName, params object[] parameter) { System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly(); UserCenterCommonForm form = (UserCenterCommonForm)assembly.CreateInstance(fullName); this.AddForm(form, parameter); } #endregion #region ■ 显示重新加载_______________________ /// /// 显示重新加载的界面(主要是用在界面加载错误时,再次加载) /// public void ShowReLoadView() { Application.RunOnMainThread(() => { var frame = new FrameLayout(); frame.BackgroundColor = UserCenterColor.Current.White; bodyFrameLayout.AddChidren(frame); //重新加载 var btnReLoad = new BottomClickButton(); btnReLoad.Gravity = Gravity.Center; btnReLoad.TextID = R.MyInternationalizationString.uDoReload; frame.AddChidren(btnReLoad); btnReLoad.MouseUpEvent += (sender, e) => { //调用关闭函数的附加函数,但是不关闭界面 this.CloseForm(false); //清除全部控件 bodyFrameLayout.RemoveAll(); //执行ShowForm()方法实现重新加载 this.LoadShowFormMethod(this.m_parameter); }; //清除topFrameLayout的非默认的控件 var list = new List(); for (int i = 0; i < topFrameLayout.ChildrenCount; i++) { var view = topFrameLayout.GetChildren(i); if (view.Name == "btnBack" || view.Name == "txtTitle") { //这里是默认的底层控件 continue; } list.Add(view); } foreach (var view in list) { view.RemoveFromParent(); } }); } #endregion #region ■ 界面重新激活事件___________________ /// /// 自身的上层界面关闭后,它自身处于最上层时,触发的事件 /// public virtual void FormActionAgainEvent() { return; } #endregion } }