Beispiel #1
0
        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);
            }
        }
Beispiel #2
0
        public void DataBindingDisconnect(ViewModelContainer rViewModelContainer)
        {
            if (!rViewModelContainer)
            {
                return;
            }

            var rAllMemberBindings = rViewModelContainer.gameObject.GetComponentsInChildren <MemberBindingAbstract>(true);

            for (int i = 0; i < rAllMemberBindings.Length; i++)
            {
                var rMemberBinding = rAllMemberBindings[i];
                if (rMemberBinding.ViewModelProp == null)
                {
                    continue;
                }

                ViewModel rViewModel = rMemberBinding.ViewModelProp.PropertyOwner as ViewModel;
                if (rViewModel != null)
                {
                    rViewModel.PropChangedHandler -= rMemberBinding.ViewModelPropertyWatcher.PropertyChanged;
                }
                rMemberBinding.OnDestroy();
            }

            var rAllEventBindings = rViewModelContainer.gameObject.GetComponentsInChildren <EventBinding>(true);

            for (int i = 0; i < rAllEventBindings.Length; i++)
            {
                rAllEventBindings[i].OnDestroy();
            }
        }
Beispiel #3
0
        /// <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);
            }
        }
Beispiel #4
0
        /// <summary>
        /// 初始化ViewController
        /// </summary>
        private async Task InitializeViewModel()
        {
            this.ViewModelContainer = this.GameObject.GetComponent <ViewModelContainer>();
            if (this.ViewModelContainer == null)
            {
                Debug.LogErrorFormat("Prefab {0} has not ViewContainer Component..", this.ViewName);
                return;
            }

            var rType = TypeResolveManager.Instance.GetType(this.ViewModelContainer.ViewModelClass);

            if (rType == null)
            {
                Debug.LogErrorFormat("Can not find ViewModel Type: {0}", rType);
                return;
            }

            // 构建ViewController
            this.ViewController      = ReflectionAssist.Construct(rType) as ViewController;
            this.ViewController.View = this;
            this.ViewController.BindingViewModels(this.ViewModelContainer);
            await this.ViewController.Initialize();

            this.ViewController.DataBindingConnect(this.ViewModelContainer);
        }
Beispiel #5
0
        /// <summary>
        /// 把ViewModel绑定到ViewController里面
        /// </summary>
        public void BindingViewModels(ViewModelContainer rViewModelContainer)
        {
            for (int i = 0; i < rViewModelContainer.ViewModels.Count; i++)
            {
                var       rViewModelDataSource = rViewModelContainer.ViewModels[i];
                ViewModel rViewModel           = null;
                Type      rViewModelType       = TypeResolveManager.Instance.GetType(rViewModelDataSource.ViewModelPath);
                if (rViewModelType != null)
                {
                    rViewModel = ReflectionAssist.Construct(rViewModelType) as ViewModel;
                }
                if (rViewModel != null)
                {
                    this.AddViewModel(rViewModelDataSource.Key, rViewModel);
                }
                else
                {
                    Debug.LogErrorFormat("Can not find ViewModel {0}.", rViewModelDataSource.ViewModelPath);
                }
            }

            // 指定ViewModel给子类的变量 通过HotfixBinding属性标签绑定
            foreach (var rPair in this.ViewModels)
            {
                ViewModel rViewModel = rPair.Value;

                var rViewModelProp = this.GetType().GetFields(ReflectionAssist.flags_public)
                                     .Where(prop =>
                {
                    var rAttrObjs = prop.GetCustomAttributes(typeof(GameBindingAttribute), false);
                    if (rAttrObjs == null || rAttrObjs.Length == 0)
                    {
                        return(false);
                    }
                    var rBindingAttr = rAttrObjs[0] as GameBindingAttribute;

                    return(prop.FieldType.IsSubclassOf(typeof(ViewModel)) &&
                           rBindingAttr != null &&
                           rBindingAttr.Name.Equals(rPair.Key));
                }).FirstOrDefault();

                if (rViewModelProp != null)
                {
                    rViewModelProp.SetValue(this, rViewModel);
                }
                else
                {
                    Debug.LogErrorFormat("ViewModel {0} is not define in ViewController({1})", rViewModel.GetType(), this.GetType());
                }
            }
        }
Beispiel #6
0
        /// <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();
                }
            }
        }
Beispiel #7
0
        /// <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);
                }
            }
        }
Beispiel #8
0
        public void DataBindingConnect(ViewModelContainer rViewModelContainer)
        {
            if (rViewModelContainer == null)
            {
                return;
            }

            // 把Event绑定到ViewController里面
            this.BindingEvents(rViewModelContainer);

            // ViewModel和View之间的数据绑定
            this.BindingViewAndViewModels(rViewModelContainer);

            // ListViewModel和View之间的数据绑定
            this.BindingListViewAndViewModels(rViewModelContainer);

            // TabViewModel和View之间的数据绑定
            this.BindingTabViewAndViewModels(rViewModelContainer);

            // ArrayViewModel和View之间的数据绑定
            this.BindingArrayViewAndViewModels(rViewModelContainer);
        }