using System; using System.Collections.Generic; namespace XGame.Framework.UI { public abstract class UIViewModule : IUIViewModel, IUIVMInternal, IDisposable { private HashSet _nestedViews; void IUIVMInternal.Init(IUIPanel panel, IUIViewAdapter adapter) { Init(panel as IComponentGetter, adapter); } void IUIVMInternal.Init(IUINested nested, IUIViewAdapter adapter) { Init(nested as IComponentGetter, adapter); } void IUIVMInternal.EnableChildren(object intent) { if (_nestedViews == null) return; foreach (var nestedView in _nestedViews) { nestedView.EnableByParent(intent); } } void IUIVMInternal.DisableChildren() { if (_nestedViews == null) return; foreach (var nestedView in _nestedViews) { nestedView.DisableByParent(); } } private void Init(IComponentGetter getter, IUIViewAdapter adapter) { BindComponents(getter, adapter); CustomBind(getter, adapter); } protected abstract void BindComponents(IComponentGetter getter, IUIViewAdapter adapter); /// /// 自定义绑定,有需要的重载 /// /// protected virtual void CustomBind(IComponentGetter getter, IUIViewAdapter adapter) { } public void AddChild(INestedView nestedView) { if (nestedView is INestedInternal temp) { _nestedViews ??= new HashSet(); _nestedViews.Add(temp); } } public void RemoveChild(INestedView nestedView) { if (_nestedViews == null) return; if (nestedView is INestedInternal temp) { _nestedViews.Remove(temp); } } void IDisposable.Dispose() { if (_nestedViews != null) { foreach (var nestedView in _nestedViews) { (nestedView as IDisposable)?.Dispose(); } _nestedViews.Clear(); } } } }