コード例 #1
0
        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;
            }

            // 清除已有的事件监听
            rListItem.PropChangedHandler = null;

            var rAllMemberBindings = rTrans.GetComponentsInChildren <MemberBindingAbstract>();

            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 = HotfixDataBindingTypeResolve.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;
                }
            }
        }
コード例 #2
0
ファイル: ViewController.cs プロジェクト: Wrenzhou/knight
        /// <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 = HotfixDataBindingTypeResolve.MakeViewModelDataBindingProperty(rMemberBinding.ViewModelPath);
                if (rMemberBinding.ViewModelProp == null)
                {
                    Debug.LogErrorFormat("View Model Path: {0} error..", rMemberBinding.ViewModelPath);
                    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();
                }
            }
        }
コード例 #3
0
        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 = HotfixDataBindingTypeResolve.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 = HotfixDataBindingTypeResolve.MakeViewModelDataBindingProperty(rMemberBinding.ViewModelPath);
                    if (rMemberBinding.ViewModelProp == null)
                    {
                        Debug.LogErrorFormat("View Model Path: {0} error..", rMemberBinding.ViewModelPath);
                        return;
                    }
                }

                // 设置类型转换器
                if (rMemberBinding.IsDataConvert)
                {
                    rMemberBinding.ViewModelProp.ConvertMethod = HotfixDataBindingTypeResolve.MakeViewModelDataBindingConvertMethod(rMemberBinding.DataConverterMethodPath);
                    if (rMemberBinding.IsDataConvert && rMemberBinding.ViewModelProp.ConvertMethod == null)
                    {
                        Debug.LogError("Cannot find convert method: {0}.." + rMemberBinding.DataConverterMethodPath);
                        return;
                    }
                }
                else
                {
                    rMemberBinding.ViewModelProp.ConvertMethod = null;
                }

                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;
                }
            }
        }