private void BindingArrayViewAndViewModels(ViewModelContainer rViewModelContainer) { var rViewModelDataSources = rViewModelContainer.gameObject.GetComponentsInChildren <ViewModelDataSourceArray>(true); for (int i = 0; i < rViewModelDataSources.Length; i++) { var rViewModelDataSource = rViewModelDataSources[i]; rViewModelDataSource.ViewModelProp = GameDataBindingTypeResolve.MakeViewModelDataBindingProperty(rViewModelDataSource.ViewModelPath); if (rViewModelDataSource.ViewModelProp == null) { Debug.LogErrorFormat("View Model Path: {0} error..", rViewModelDataSource.ViewModelPath); return; } ViewModel rViewModel = this.GetViewModel(rViewModelDataSource.ViewModelProp.PropertyOwnerKey); if (rViewModel == null) { Debug.LogErrorFormat("View Model: {0} error..", rViewModelDataSource.ViewModelPath); return; } rViewModelDataSource.ViewModelProp.PropertyOwner = rViewModel; // 绑定watcher rViewModelDataSource.ViewModelPropertyWatcher = new DataBindingPropertyWatcher(rViewModel, rViewModelDataSource.ViewModelProp.PropertyName, () => { this.FillArrayItems(rViewModelDataSource); }); rViewModel.PropChangedHandler += rViewModelDataSource.ViewModelPropertyWatcher.PropertyChanged; this.FillArrayItems(rViewModelDataSource); } }
/// <summary> /// ListViewModel和View之间的数据绑定 /// </summary> private void BindingListViewAndViewModels(ViewModelContainer rViewModelContainer) { var rViewModelDataSources = UtilTool.GetComponentsInChildrenUtilOrigin <ViewModelDataSourceList>(rViewModelContainer); for (int i = 0; i < rViewModelDataSources.Count; i++) { var rViewModelDataSource = rViewModelDataSources[i]; rViewModelDataSource.ViewModelProp = GameDataBindingTypeResolve.MakeViewModelDataBindingProperty(rViewModelDataSource.ViewModelPath); if (rViewModelDataSource.ViewModelProp == null) { Debug.LogErrorFormat("View Model Path: {0} error..", rViewModelDataSource.ViewModelPath); return; } ViewModel rViewModel = this.GetViewModel(rViewModelDataSource.ViewModelProp.PropertyOwnerKey); if (rViewModel == null) { Debug.LogErrorFormat("View Model: {0} error..", rViewModelDataSource.ViewModelPath); return; } rViewModelDataSource.ViewModelProp.PropertyOwner = rViewModel; // 绑定Watcher rViewModelDataSource.ViewModelPropertyWatcher = new DataBindingPropertyWatcher(rViewModel, rViewModelDataSource.ViewModelProp.PropertyName, () => { // 重新设置List数据时候,改变个数 this.BindingList(rViewModelDataSource); }); rViewModel.PropChangedHandler += rViewModelDataSource.ViewModelPropertyWatcher.PropertyChanged; // 初始化list this.BindingList(rViewModelDataSource); } }
/// <summary> /// ViewModel和View之间的数据绑定 /// </summary> private void BindingViewAndViewModels(ViewModelContainer rViewModelContainer) { var rAllMemberBindings = UtilTool.GetComponentsInChildrenUtilOrigin <MemberBindingAbstract>(rViewModelContainer); for (int i = 0; i < rAllMemberBindings.Count; i++) { var rMemberBinding = rAllMemberBindings[i]; if (rMemberBinding.IsListTemplate) { continue; // 过滤掉ListTemplate标记得Binding Script } rMemberBinding.ViewProp = DataBindingTypeResolve.MakeViewDataBindingProperty(rMemberBinding.gameObject, rMemberBinding.ViewPath); if (rMemberBinding.ViewProp == null) { Debug.LogErrorFormat("View Path: {0} error..", rMemberBinding.ViewPath); return; } rMemberBinding.ViewModelProp = GameDataBindingTypeResolve.MakeViewModelDataBindingProperty(rMemberBinding.ViewModelPath); if (rMemberBinding.ViewModelProp == null) { Debug.LogErrorFormat("View Model Path: {0}, {1} error..", rMemberBinding.ViewModelPath, rMemberBinding); return; } ViewModel rViewModel = this.GetViewModel(rMemberBinding.ViewModelProp.PropertyOwnerKey); if (rViewModel == null) { Debug.LogErrorFormat("View Model: {0} error..", rMemberBinding.ViewModelPath); return; } rMemberBinding.ViewModelProp.PropertyOwner = rViewModel; rMemberBinding.SyncFromViewModel(); // ViewModel绑定View rMemberBinding.ViewModelPropertyWatcher = new DataBindingPropertyWatcher(rViewModel, rMemberBinding.ViewModelProp.PropertyName, () => { rMemberBinding.SyncFromViewModel(); }); rViewModel.PropChangedHandler += rMemberBinding.ViewModelPropertyWatcher.PropertyChanged; // View绑定ViewModel var rMemberBindingTwoWay = rMemberBinding as MemberBindingTwoWay; if (rMemberBindingTwoWay != null) { rMemberBindingTwoWay.InitEventWatcher(); } } }
/// <summary> /// 把Event绑定到ViewController里面 /// </summary> private void BindingEvents(ViewModelContainer rViewModelContainer) { for (int i = 0; i < rViewModelContainer.EventBindings.Count; i++) { var rEventBinding = rViewModelContainer.EventBindings[i]; if (rEventBinding.IsListTemplate) { continue; } var bResult = GameDataBindingTypeResolve.MakeViewModelDataBindingEvent(this, rEventBinding); if (!bResult) { Debug.LogErrorFormat("Make view model binding event {0} failed..", rEventBinding.ViewModelMethod); } } }
private void OnListViewFillCellFunc(Transform rTrans, int nIndex, IList rListObj) { if (rListObj == null || nIndex >= rListObj.Count) { return; } var rListItem = rListObj[nIndex] as ViewModel; if (rListItem == null) { return; } var rAllEventBindings = rTrans.GetComponentsInChildren <EventBinding>(true); for (int i = 0; i < rAllEventBindings.Length; i++) { var rEventBinding = rAllEventBindings[i]; if (!rEventBinding.IsListTemplate) { continue; } rEventBinding.OnDestroy(); var bResult = GameDataBindingTypeResolve.MakeListViewModelDataBindingEvent(this, rEventBinding, nIndex); if (!bResult) { Debug.LogErrorFormat("Make view model binding event {0} failed..", rEventBinding.ViewModelMethod); } } // 清除已有的事件监听 rListItem.PropChangedHandler = null; var rAllMemberBindings = rTrans.GetComponentsInChildren <MemberBindingAbstract>(true); for (int i = 0; i < rAllMemberBindings.Length; i++) { var rMemberBinding = rAllMemberBindings[i]; if (!rMemberBinding.IsListTemplate) { continue; // 过滤掉非ListTemplate标记的Binding Script } if (rMemberBinding.ViewProp == null) { rMemberBinding.ViewProp = DataBindingTypeResolve.MakeViewDataBindingProperty(rMemberBinding.gameObject, rMemberBinding.ViewPath); } if (rMemberBinding.ViewProp == null) { Debug.LogErrorFormat("List template View Path: {0} error..", rMemberBinding.ViewPath); return; } if (rMemberBinding.ViewModelProp == null) { rMemberBinding.ViewModelProp = GameDataBindingTypeResolve.MakeViewModelDataBindingProperty(rMemberBinding.ViewModelPath); if (rMemberBinding.ViewModelProp == null) { Debug.LogErrorFormat("View Model Path: {0} error..", rMemberBinding.ViewModelPath); return; } } rMemberBinding.ViewModelProp.PropertyOwner = rListItem; rMemberBinding.SyncFromViewModel(); if (rListItem != null) { // ViewModel绑定View rMemberBinding.ViewModelPropertyWatcher = new DataBindingPropertyWatcher(rListItem, rMemberBinding.ViewModelProp.PropertyName, () => { rMemberBinding.SyncFromViewModel(); }); rListItem.PropChangedHandler += rMemberBinding.ViewModelPropertyWatcher.PropertyChanged; } } }