using System; using System.Collections.Generic; using Shared.IO; using UIKit; namespace Shared { /// /// ViewGroup 按键 /// public class ViewGroup : View { /// /// 当前真实的控件 /// internal UIView realViewGroup; /// /// 当前控件 /// /// The view group. internal UIView viewGroup { get { return uiView as UIView; } set { uiView = value; uiView.AddSubview(backgroundImage); } } /// /// 当前背景图 /// UIKit.UIImageView backgroundImage = new UIImageView() { Tag = int.MinValue}; /// /// 控件宽度 /// public override int Width { get { return base.Width; } set { base.Width = value; if (!IsCanRefresh) return; var frame = backgroundImage.Frame; frame.Width = base.Width; backgroundImage.Frame = frame; var frame2 = realViewGroup.Frame; frame2.Width = base.Width; realViewGroup.Frame = frame2; } } /// /// 控件的高度 /// /// The height. public override int Height { get { return base.Height; } set { base.Height = value; if (!IsCanRefresh) return; var frame = backgroundImage.Frame; frame.Height = base.Height; backgroundImage.Frame = frame; var frame2 = realViewGroup.Frame; frame2.Height = base.Height; realViewGroup.Frame = frame2; } } /// /// 增加子控件 /// /// View. public virtual void AddChidren(View view) { view.Parent = this; viewList.Add(view); realViewGroup.AddSubview(view.RealView); if (!IsCanRefresh) { return; } view.Refresh(); addParentToAllSubViews(view); if (view is ViewGroup) { var tempViewGroup = (ViewGroup)view; for (int i = 0; i < tempViewGroup.ChildrenCount; i++) { tempViewGroup.GetChildren(i).Refresh(); } } } internal void addParentToAllSubViews(View view) { if (view is ViewGroup) { var viewgroup = view as ViewGroup; for (int i = 0; i < viewgroup.viewList.Count; i++) { var _view = viewgroup.viewList[i]; if (_view == null) continue; _view.Parent = viewgroup; addParentToAllSubViews(_view);//递归,将子控件所包含的控件也一并移除; } } } /// /// 控件数量 /// /// The children count. public virtual int ChildrenCount { get { return viewList.Count; } } /// /// 控件列表 /// internal System.Collections.Generic.List viewList = new System.Collections.Generic.List(); string backgroundImagePath; /// /// 背景图片 /// /// The background image path. public virtual string BackgroundImagePath { get { return backgroundImagePath; } set { backgroundImagePath = value; if (!IsCanRefresh || Radius != 0) { return; } backgroundImage.Image = UIImage.FromFile(FileUtils.GetImageFilePath(backgroundImagePath)); } } /// /// 刷新界面 /// public override void Refresh() { base.Refresh(); BackgroundImagePath = backgroundImagePath; } /// /// 移除控件 /// /// View. internal virtual void Remove(View view) { if (view == null) { return; } viewList.Remove(view); if (view.RealView.Superview != null) { view.RealView.RemoveFromSuperview(); } view.Parent = null; } internal virtual void removeChildParent() { for (int i = 0; i < viewList.Count; i++) { var view = viewList[i]; if (view == null) continue; if (view is ViewGroup) { (view as ViewGroup).removeChildParent(); } view.Parent = null; } } public override void RemoveFromParent() { removeChildParent(); base.RemoveFromParent(); } /// /// 清空所有的控件 /// public virtual void RemoveAll() { while (0 < viewList.Count) { GetChildren(0)?.RemoveFromParent(); } } /// /// 移除指定索引对象 /// /// Index. public virtual void RemoveAt(int index) { //Remove(GetChildren(index)); GetChildren(index)?.RemoveFromParent(); } /// /// 获取指定索引对象 /// /// The children. /// Index. public View GetChildren(int index) { if (index < 0 || viewList.Count - 1 < index) { return null; } return viewList[index]; } /// /// 根据类型移除控件 /// /// Type. public void RemoveViewByType(System.Type type) { for (int i = 0; i < viewList.Count; i++) { if (viewList[i].GetType() == type) { viewList[i].RemoveFromParent(); i--; } } } /// /// 根据Tag移除控件 /// /// Type. public void RemoveViewByTag(object tag) { for (int i = 0; i < viewList.Count; i++) { if (viewList[i].Tag == tag) { viewList[i].RemoveFromParent(); i--; } } } } }