Unity3D开发技术研究构建框架PureMVC项目之APP界面应用(下篇)

一、案例说明

本篇承接上篇为基础,针对UI开发一个以PureMVC为框架思想的“增删改查”APP界面应用。

二、框架视图

三、关键代码

GameStart

/***
 * 
 *    Title: "App应用" 项目
 *           主题:使用PureMVC 架构App示例应用项目 
 *
 *           启动PureMVC框架
 * 
 *    Description: 
 *           功能:
 *                  
 *    Date: 2021.4.9
 *    Version: 0.1版本
 *    Modify Recoder: 
 *    
 *   
 */
using System.Collections;
using System.Collections.Generic;
using SUIFW;
using UnityEngine;

namespace AppByPureMVC
{
    public class GameStart:MonoBehaviour
    {
        //public UserList userListObj;
        public UserEmployeeInfo UseEmpInfo;

        void Start()
        {
            Log.Write(GetType()+"/Start()");
            //启动框架
            ApplicationFacade appFacade = ApplicationFacade.Instance as ApplicationFacade;
            //启动项目
            if (appFacade != null && UseEmpInfo != null)
            {
                appFacade.SendNotification(ProConsts.Com_InitMediator, UseEmpInfo);
            }
        }
    }
}

ApplicationFacade

/***
 * 
 *    Title: "App应用" 项目
 *           主题:使用PureMVC 架构App示例应用项目 
 *
 *           PureMvc 框架入口程序
 * 
 *    Description: 
 *           功能:
 *                  
 *     Date: 2021.4.9
 *    Version: 0.1版本
 *    Modify Recoder: 
 *    
 *   
 */
using System.Collections;
using System.Collections.Generic;
using PureMVC.Interfaces;
using PureMVC.Patterns;
using SUIFW;
using UnityEngine;

namespace AppByPureMVC
{
    public class ApplicationFacade:Facade {

        protected ApplicationFacade()
        {
             //空
            Log.Write(GetType() + "/构造");
        }

        /// 
        /// PureMVC 得到实例引用
        /// 
        /// 得到“单例”且“线程安全”的引用
        /// 
        public new static IFacade Instance
        {
            get
            {
                if (m_instance==null)
                {
                    lock (m_staticSyncRoot)
                    {
                        if (m_instance==null)
                        {
                            m_instance=new ApplicationFacade();
                        }
                    }
                }
                return m_instance;
            }

        }


        /// 
        /// 注册模型层实例
        /// 
        protected override void InitializeModel()
        {
            base.InitializeModel();
            RegisterProxy(new UserProxy());
        }

        /// 
        /// 注册视图层实例
        /// 
        protected override void InitializeView()
        {
            base.InitializeView();
            RegisterMediator(new UserListMediator());
            RegisterMediator(new UserFormMediator());
        }

        /// 
        /// 注册控制层实例
        /// 
        protected override void InitializeController()
        {
            base.InitializeController();
            RegisterCommand(ProConsts.Com_InitMediator,typeof(StartupApplication));
        }
    }
}

ProConsts

/***
 * 
 *    Title: "App应用" 项目
 *           主题:使用PureMVC 架构App示例应用项目 
 *
 *           定义项目中所有的“常量”
 * 
 *    Description: 
 *           功能:
 *                  
 *    Date: 2021.4.9
 *    Version: 0.1版本
 *    Modify Recoder: 
 *    
 *   
 */
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace AppByPureMVC
{
    public class ProConsts {
        /*  PureMVC 命令消息 */
        public const string Com_InitMediator="Com_InitMediator";
        /*  PureMVC 通知消息 */
        public const string Msg_InitUserListMediator = "Msg_InitUserListMediator";
        public const string Msg_InitUserFormMediator = "Msg_InitUserFormMediator";
        //增加用户信息
        public const string Msg_AddNewUserInfo = "Msg_AddNewUserInfo";
        //清除用户信息
        public const string Msg_ClearUserInfo = "Msg_ClearUserInfo";
        //发送用户列表Mediator消息_增加用户记录
        public const string Msg_AddNewUserInfoToUserListMed = "Msg_AddNewUserInfoToUserListMed";
        //发送用户列表Mediator消息_更新用户记录
        public const string Msg_UpdateUserInfoToUserListMed = "Msg_UpdateUserInfoToUserListMed";
        //发送到列表信息Mediator 关于用户选择信息
        public const string Msg_SelectUserInfoByCtrlToUserListMediator = "Msg_SelectUserInfoByCtrlToUserListMediator";
        //发送到“用户窗体”Mediator类。
        public const string Msg_SelUserInfoByUserListMedToUserFormMed = "Msg_SelUserInfoByUserListMedToUserFormMed";

    }
}

UserEmployeeInfo

/***
 * 
 *    Title: "App应用" 项目
 *           主题:使用PureMVC 架构App示例应用项目 
 *
 *           帮助脚本
 * 
 *    Description: 
 *           功能:
 *                  
 *    Date: 2021.4.9
 *    Version: 0.1版本
 *    Modify Recoder: 
 *    
 *   
 */
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace AppByPureMVC
{
    public class UserEmployeeInfo:MonoBehaviour
    {
        public UserList UserListobj;
        public UserForm UserFormobj;

    }
}
逻辑控制层 Control

StartupApplication

/***
 * 
 *    Title: "App应用" 项目
 *           主题:使用PureMVC 架构App示例应用项目 
 *
 *           控制层: 初始化用户列表Mediator 
 * 
 *    Description: 
 *           功能:
 *                  
 *     Date: 2021.4.9
 *    Version: 0.1版本
 *    Modify Recoder: 
 *    
 *   
 */
using System.Collections;
using System.Collections.Generic;
using PureMVC.Interfaces;
using PureMVC.Patterns;
using SUIFW;
using UnityEngine;

namespace AppByPureMVC
{
    public class StartupApplication:SimpleCommand
    {
        //private UserList userListObj = null;
        private UserEmployeeInfo userEmpInfo = null;
        
        public override void Execute(INotification notification)
        {
            Log.Write(GetType() + "/Execute");
            userEmpInfo = notification.Body as UserEmployeeInfo;
            if (userEmpInfo != null)
            {
                //初始化用户列表操作类
                SendNotification(ProConsts.Msg_InitUserListMediator, userEmpInfo.UserListobj);
                //初始化用户窗体操作类
                SendNotification(ProConsts.Msg_InitUserFormMediator, userEmpInfo.UserFormobj);

            }
        }
    }
}
数据模型层 Mode

UserProxy

/***
 * 
 *    Title: "App应用" 项目
 *           主题:使用PureMVC 架构App示例应用项目 
 *           模型层:代理类
 *    Description: 
 *           功能: 针对数据实体类,进行各种操作。
 *                  
 *     Date: 2021.4.9
 *    Version: 0.1版本
 *    Modify Recoder: 
 *    
 *   
 */
using System.Collections;
using System.Collections.Generic;
using PureMVC.Patterns;
using SUIFW;
using UnityEngine;
using UnityEngine.AI;

namespace AppByPureMVC
{
    public class UserProxy:Proxy {
        //类名称
        public new const string NAME = "UserProxy";
        //只读属性
        public IList Users
        {
            get { return base.Data as IList; }
        }

        //构造函数
        public UserProxy():base(NAME,new List())
        {
            Log.Write(GetType() + "/构造");
            AddUserItem(new UserVO("陆","毅",true,"技术研发部","01012345678","[email protected]"));
            AddUserItem(new UserVO("杨", "幂", false, "销售市场部", "13166668888", "[email protected]"));
            AddUserItem(new UserVO("angle", "baby", false, "市场部", "01012345678", "[email protected]"));
            AddUserItem(new UserVO("吴", "一凡", true, "技术研发部", "01012345678", "[email protected]"));
            AddUserItem(new UserVO("沙", "宝亮", true, "技术研发部", "01012345678", "[email protected]"));
            Log.Write(GetType() + "/Users.count="+Users.Count);
        }

        //增加用户数据
        public void AddUserItem(UserVO user)
        {
            if (user!=null)
            {
                Users.Add(user);
            }
        }

        //更新数据
        public void UpdateUserItems(UserVO user)
        {
            for (int i = 0; i < Users.Count; i++)
            {
                if (Users[i].Equals(user))
                {
                    //更新
                    Users[i] = user;
                    break;
                }
            }
        }

        //删除数据
        public void DeleteUserItems(UserVO user)
        {
            for (int i = 0; i < Users.Count; i++)
            {
                if (Users[i].Equals(user))
                {
                    Users.RemoveAt(i);
                    break;                  
                }
            }
        }



    }
}

UserVO

/***
 * 
 *    Title: "App应用" 项目
 *           主题:使用PureMVC 架构App示例应用项目
 *           
 *           模型层: 数据实体类
 *
 *    Description: 
 *           功能:
 *                  
 *    Date: 2021.4.9
 *    Version: 0.1版本
 *    Modify Recoder: 
 *    
 *   
 */
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace AppByPureMVC
{
    public class UserVO {
        //字段
        private string _FistName;                           //姓
        private string _LastName;                           //名字
        private bool _Gender;                               //性别
        private string _Department;                         //部门
        private string _Telephone;                          //联系方式
        private string _Email;                              //邮箱

        //属性
        public string UserName
        {
            get { return _FistName + _LastName; }
        }
        public string FirstName
        {
            set { _FistName = value; }
            get { return _FistName ; }
        }
        public string LastName
        {
            set { _LastName = value; }
            get { return _LastName; }
        }     
        public bool Gender
        {
            set { _Gender = value; }
            get { return _Gender; }
        }
        public string Department
        {
            set { _Department = value; }
            get { return _Department; }
        }
        public string Telephone
        {
            set { _Telephone = value; }
            get { return _Telephone; }
        }
        public string Email
        {
            set { _Email = value; }
            get { return _Email; }
        }

        //只读属性: 有效性
        public bool IsValid
        {
            get
            {
                return !string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(Department);
            }
        }

        public UserVO()
        {
            //空
        }

        public UserVO(string fname,string lname,bool gender,string dep,string tel,string email)
        {
            if (!string.IsNullOrEmpty(fname)) _FistName = fname;
            if (!string.IsNullOrEmpty(lname)) _LastName = lname;
            _Gender = gender;
            if (!string.IsNullOrEmpty(dep)) _Department = dep;
            if (!string.IsNullOrEmpty(tel)) _Telephone = tel;
            if (!string.IsNullOrEmpty(email)) _Email = email;
        }

        //对象比较
        public override bool Equals(object other)
        {
            bool boolResult = false;                        //返回结果
            UserVO otherVO = null;

            otherVO = other as UserVO;
            if (otherVO!=null)
            {
                if (this.UserName==otherVO.UserName)
                {
                    boolResult = true;
                }
            }

            return boolResult;
        }
    }
}
视图交互层 View

UserListMediator

/***
 * 
 *    Title: "App应用" 项目
 *           主题:使用PureMVC 架构App示例应用项目 
 *           
 *           视图层:用户列表信息操作类
 *
 *    Description: 
 *           功能:
 *                  
 *     Date: 2021.4.9
 *    Version: 0.1版本
 *    Modify Recoder: 
 *    
 *   
 */
using System.Collections;
using System.Collections.Generic;
using PureMVC.Interfaces;
using SUIFW;
using UnityEngine;
using PureMVC.Patterns;

namespace AppByPureMVC
{
    public class UserListMediator : Mediator
    {
        //字段
        public new const string NAME = "UserListMediator"; //本类名称
        private UserProxy _UserProxy; //模型层,用户信息代理类
        //(只读)属性
        private UserList _UserListProp
        {
            get
            {
                return base.ViewComponent as UserList;
            }
        }
        //当前用户选择的记录
        private UserVO _CurrentSelectUserRecord = null;


        public UserListMediator():base(NAME)
        {
            Log.Write(GetType() + "/构造");
            //构造方法为空。
        }

        /// 
        /// 初始化用户列表Mediator
        /// 
        /// 用户列表脚本
        internal void InitUserListMediator(UserList userListObj)
        {
            if (userListObj != null)
            {
                Log.Write(GetType() + "/InitUserListMediator()");
                base.m_mediatorName = NAME;
                base.m_viewComponent = userListObj;

                //委托注册
                userListObj.NewUser += HandleNewUser;
                userListObj.DeleteUser += HandleDeleteUser;
            }
            //加载且显示初始视图内容
            Log.Write(GetType() + "/InitUserListMediator()/_UserProxy.Users.count="+_UserProxy.Users.Count);
            userListObj.LoadAndShowUserListInfo(_UserProxy.Users);
        }

        /// 
        /// 本类实例在注册时候触发
        /// 
        public override void OnRegister()
        {
            base.OnRegister();
            //得到“用户代理”引用
            _UserProxy = Facade.RetrieveProxy(UserProxy.NAME) as UserProxy;
        }


        /// 
        /// 所有关注的“消息”
        /// 
        /// 
        public override IList ListNotificationInterests()
        {
            IList list = new List();
            //初始化本类实例
            list.Add(ProConsts.Msg_InitUserListMediator);
            //本类(下级)控制脚本发来的选择信息
            list.Add(ProConsts.Msg_SelectUserInfoByCtrlToUserListMediator);
            //从UserFormMediate 发来信息:增加新用户
            list.Add(ProConsts.Msg_AddNewUserInfoToUserListMed);
            //从UserFormMediate 发来信息:更新用户信息
            list.Add(ProConsts.Msg_UpdateUserInfoToUserListMed);
            return list;
        }

        /// 
        /// 处理所有关注的“消息”
        /// 
        /// 
        public override void HandleNotification(INotification notification)
        {
            switch (notification.Name)
            {
                //调用初始化方法
                case ProConsts.Msg_InitUserListMediator:
                    UserList userListObj = notification.Body as UserList;
                    InitUserListMediator(userListObj);
                    break;

                //处理用户选择的用户记录
                case ProConsts.Msg_SelectUserInfoByCtrlToUserListMediator:
                    UserVO  userVo = notification.Body as UserVO;
                    HandelSelctUserInfo(userVo);
                    break;

                //从UserFormMediate 发来信息:增加新用户
                case ProConsts.Msg_AddNewUserInfoToUserListMed:
                    UserVO userVoObj = notification.Body as UserVO;
                    SubmitNewUser(userVoObj);
                    break;
                //从UserFormMediate 发来信息:更新用户信息
                case ProConsts.Msg_UpdateUserInfoToUserListMed:
                    UserVO userVoObj2 = notification.Body as UserVO;
                    SubmitUpdateUserInfo(userVoObj2);
                    break;

                default:
                    break;
            }
        }

        #region 私有方法

        //处理“选择用户操作”
        private void HandelSelctUserInfo(UserVO userVO)
        {
            if (userVO!=null)
            {
                //保存当前用户选择的记录。
                _CurrentSelectUserRecord = userVO;
                //显示窗体“删除”按钮
                _UserListProp.ShowDeleteButton();
                //发送消息,发送“用户窗体”类。
                SendNotification(ProConsts.Msg_SelUserInfoByUserListMedToUserFormMed, userVO);
            }
        }

        //处理“新用户”操作
        private void HandleNewUser()
        {
            //发送消息,给“用户(窗体)表单”视图。
            SendNotification(ProConsts.Msg_ClearUserInfo);
        }

        //处理“删除用户”操作
        private void HandleDeleteUser()
        {
            if (_UserProxy!=null && _CurrentSelectUserRecord!=null)
            {
                //代理层,调用删除方法
                _UserProxy.DeleteUserItems(_CurrentSelectUserRecord);
                //刷新“用户列表”
                _UserListProp.LoadAndShowUserListInfo(_UserProxy.Users);
                //通知“用户窗体”操作类,清空窗体信息
                SendNotification(ProConsts.Msg_ClearUserInfo);
            }
        }

        //提交(处理)增加“新用户”操作
        private void SubmitNewUser(UserVO newUserVo)
        {
            //调用模型层代理方法,增加一条记录
            if (_UserProxy!=null && newUserVo!=null)
            {
                _UserProxy.AddUserItem(newUserVo);
                //刷新窗体信息
                _UserListProp.LoadAndShowUserListInfo(_UserProxy.Users);
            }
        }

        //提交(处理)更新用户信息操作
        private void SubmitUpdateUserInfo(UserVO needUpdateUserVo)
        {
            //调用模型层代理方法,增加一条记录
            if (_UserProxy != null && needUpdateUserVo != null)
            {
                _UserProxy.UpdateUserItems(needUpdateUserVo);
                //刷新窗体信息
                _UserListProp.LoadAndShowUserListInfo(_UserProxy.Users);
            }
        }

        #endregion
    }
}

UserFormMediator

/***
 * 
 *    Title: "App应用" 项目
 *           主题:使用PureMVC 架构App示例应用项目 
 *
 *           视图层: 用户窗体操作类
 * 
 *    Description: 
 *           功能:
 *                  
 *    Date: 2021.4.9
 *    Version: 0.1版本
 *    Modify Recoder: 
 *    
 *   
 */
using System.Collections;
using System.Collections.Generic;
using PureMVC.Interfaces;
using PureMVC.Patterns;
using UnityEngine;

namespace AppByPureMVC
{
    /// 
    /// 用户类型
    /// 
    public enum UserFormType
    {
        //创建(新用户)
        Create,
        //更新
        Update
    }

    public class UserFormMediator:Mediator
    {
        public new const string NAME = "UserFormMediator";
        //操作模式
        private UserFormType _UserFromType = UserFormType.Create;
        //(只读)属性
        private UserForm _UserForm_Prop
        {
            get
            {
                return base.ViewComponent as UserForm;
            }
        }

        /// 
        /// 构造函数
        /// 
        public UserFormMediator():base(NAME)
        {
            //空
        }

        /// 
        /// 初始化方法
        /// 
        /// 
        internal void InitUserFormMediator(UserForm userFormObj)
        {
            if (userFormObj!=null)
            {
                base.m_mediatorName = NAME;
                base.m_viewComponent = userFormObj;
                //委托注册
                _UserForm_Prop.HandlerComfirmButton = SumitUserInfo;

            }
        }

        /// 
        /// 关注消息内容
        /// 
        /// 
        public override IList ListNotificationInterests()
        {
            IList list=new List();
            //初始化本类实例
            list.Add(ProConsts.Msg_InitUserFormMediator);
            //接受用户选择的用户信息
            list.Add(ProConsts.Msg_SelUserInfoByUserListMedToUserFormMed);
            //增加一条用户信息
            list.Add(ProConsts.Msg_AddNewUserInfo);
            //清除用户信息
            list.Add(ProConsts.Msg_ClearUserInfo);

            return list;
        }

        /// 
        /// 处理消息通知
        /// 
        /// 
        public override void HandleNotification(INotification notification)
        {
            switch (notification.Name)
            {
                //初始化信息
                case ProConsts.Msg_InitUserFormMediator:
                    UserForm userFormObj=notification.Body as UserForm;
                    InitUserFormMediator(userFormObj);
                    break;

                //用户选择信息
                case ProConsts.Msg_SelUserInfoByUserListMedToUserFormMed:
                    _UserFromType = UserFormType.Update;
                    UserVO userVoObj = notification.Body as UserVO;
                    DisplayAndUpdateUserForm(UserFormType.Update,userVoObj);
                    break;

                //增加用户信息
                case ProConsts.Msg_AddNewUserInfo:
                    _UserFromType = UserFormType.Create;
                    DisplayAndUpdateUserForm(UserFormType.Create);
                    break;

                //清除用户信息
                case ProConsts.Msg_ClearUserInfo:
                    _UserFromType = UserFormType.Create;
                    _UserForm_Prop.ClearForm();
                    break;
                default:
                    break;
            }
        }

        /// 
        /// 显示: 更新与显示用户的窗体信息
        /// 
        /// 
        /// 
        private void DisplayAndUpdateUserForm(UserFormType showType,UserVO userVoObj=null)
        {
            if (_UserForm_Prop!=null)
            {
                _UserForm_Prop.ShowUserFormInfo(showType,userVoObj);
            }
        }

        /// 
        /// 提交: 用户点击“确认”按钮,提交信息
        /// 
        private void SumitUserInfo()
        {
            switch (_UserFromType)
            {
                case UserFormType.Create:
                    AddNewUserInfo();
                    break;
                case UserFormType.Update:
                    if (_UserForm_Prop.UserVO!=null)
                    {
                        UpdateUserFrom(_UserForm_Prop.UserVO);                    
                    }
                    break;
                default:
                    break;
            }
        }

        /// 
        /// 提交:增加新用户
        ///   功能:往“用户列表”Mediatro 发送消息,增加一条新记录,且显示。
        /// 
        private void AddNewUserInfo()
        {
            SendNotification(ProConsts.Msg_AddNewUserInfoToUserListMed,_UserForm_Prop.UserVO);
        }

        /// 
        /// 提交:更新用户信息
        ///   功能:往“用户列表”Mediatro 发送消息,更新指定记录信息且显示。
        /// 
        private void UpdateUserFrom(UserVO uservoObj)
        {
            SendNotification(ProConsts.Msg_UpdateUserInfoToUserListMed, uservoObj);
        }
    }
}

UserForm

/***
 * 
 *    Title: "App应用" 项目
 *           主题:使用PureMVC 架构App示例应用项目 
 *           
 *           视图层: 用户信息窗体
 *
 *    Description: 
 *           功能: 显示用户的单条信息(为了查看、更新)
 *                  
 *    Date: 2021.4.9
 *    Version: 0.1版本
 *    Modify Recoder: 
 *    
 *   
 */
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace AppByPureMVC
{
    public class UserForm:MonoBehaviour{
        //委托
        public System.Action HandlerComfirmButton;
        //用户控件
        public Button BtnComfirm;                           //确认按钮
        public InputField Inp_FirstName;
        public InputField Inp_LastName;
        public Toggle Tog_Male;
        public Toggle Tog_Famale;
        public InputField Inp_Department;
        public InputField Inp_Telphone ;
        public InputField Inp_Email;
        //用户实体类
        private UserVO _UserVO;
        //(只读)属性
        public UserVO UserVO
        {
            get { return _UserVO; }
        }


        void Start()
        {
            //注册按钮事件
            BtnComfirm.onClick.AddListener(HandleConfirmButtonClick);
            //冻结确认按钮
            FreezeConfirmButton();
            //控件注册
            Inp_FirstName.onValueChanged.AddListener(ClickInputControl);
        }

        /// 
        /// 显示用户窗体信息
        /// 
        /// 
        /// 
        public void ShowUserFormInfo(UserFormType showType,UserVO userVo=null)
        {
            switch (showType)
            {
                case UserFormType.Create:
                    ClearForm();
                   break;
                case UserFormType.Update:
                    ShowUserFormInfo(userVo);
                   break;
                default:
                    break;
            }
        }

        /// 
        /// 显示用户窗体信息
        /// 
        /// 
        private  void ShowUserFormInfo(UserVO userVoObj)
        {
            if (userVoObj!=null)
            {
                _UserVO = userVoObj;
                //控件显示
                Inp_FirstName.text = userVoObj.FirstName;
                Inp_LastName.text = userVoObj.LastName;
                if (userVoObj.Gender)
                {
                    Tog_Male.isOn = true;
                }
                else
                {
                    Tog_Famale.isOn = true;
                }
                Inp_Department.text = userVoObj.Department;
                Inp_Telphone.text = userVoObj.Telephone;
                Inp_Email.text = userVoObj.Email;
            }
        }

        /// 
        /// 清空窗体
        /// 
        public void ClearForm()
        {
            _UserVO = null;
            Inp_FirstName.text = "";
            Inp_LastName.text = "";
            Inp_Department.text = "";
            Inp_Telphone.text = "";
            Inp_Email.text = "";
            //冻结“确认”按钮
            FreezeConfirmButton();
        }



        #region 私有方法

        /// 
        /// 处理“确认”按钮
        /// 
        private void HandleConfirmButtonClick()
        {
            //窗体数据合法性检查
            if (!CheckUserForm())
            {
                return;
            }
            //调用委托
            if (HandlerComfirmButton!=null)
            {
                HandlerComfirmButton.Invoke();
            }
        }

        /// 
        /// 检查用户窗体信息(即:不允许用户输入“空”信息)
        /// 返回:true 表示数据合法
        /// 
        /// 
        private bool CheckUserForm()
        {
            if (_UserVO==null)
            {
                _UserVO=new UserVO();
            }
            //获取数据 
            _UserVO.FirstName = Inp_FirstName.text;
            _UserVO.LastName = Inp_LastName.text;
            if (Tog_Male.isOn)
            {
                _UserVO.Gender = true;
            }
            else if (Tog_Famale.isOn)
            {
                _UserVO.Gender = false;
            }
            _UserVO.Department = Inp_Department.text;
            _UserVO.Telephone = Inp_Telphone.text;
            _UserVO.Email = Inp_Email.text;
            //数据检查
            if (_UserVO.IsValid)
            {
                return true;
            }

            return false;
        }

        /// 
        /// 冻结确认按钮
        /// 
        private void FreezeConfirmButton()
        {
            BtnComfirm.interactable = false;
        }

        /// 
        /// 显示“确认”按钮
        /// 
        private void ShowConfirmButton()
        {
            BtnComfirm.interactable = true;
        }

        //检查控件输入信息
        private void ClickInputControl(string strInput)
        {
            if (!string.IsNullOrEmpty(strInput))
            {
                //允许显示
                ShowConfirmButton();
            }
        }


        #endregion


    }
}

UserList

/***
 * 
 *    Title: "App应用" 项目
 *           主题:使用PureMVC 架构App示例应用项目 
 *
 *                 视图层: 用户列表信息脚本
 * 
 *    Description: 
 *           功能:
 *                  
 *    Date: 2021.4.9
 *    Version: 0.1版本
 *    Modify Recoder: 
 *    
 *   
 */
using System.Collections;
using System.Collections.Generic;
//using System.Net.Mime;
//using Boo.Lang;
using UnityEngine;
using UnityEngine.UI;

namespace AppByPureMVC
{
    public class UserList:MonoBehaviour{
        //系统委托
        public System.Action NewUser;                       //新用户
        public System.Action SelectUser;                    //选择用户信息
        public System.Action DeleteUser;                    //删除用户信息
        //用户列表
        public UserList_Item UserListItemPrefab;
        //文本控件
        public Text TxtUseListNumber;                       //用户列表数量
        //按钮控件
        public Button btn_New;                              //新建用户控件
        public Button btn_Delete;                           //删除用户控件
        //用户列表信息集合
        private List _UserListInfo = new List();



        void Start()
        {
            //初始化字段
            TxtUseListNumber.text = "0";
            //设置隐藏
            UserListItemPrefab.gameObject.SetActive(false);
            //按钮事件注册
            btn_New.onClick.AddListener(ClickBtn_New);
            btn_Delete.onClick.AddListener(ClickBtn_Delete);
            //默认,冻结删除按钮
            FreezeDeleteButton();
        }
  
        /// 
        /// 提取显示用户列表信息
        /// 
        /// 需要信息的信息集合
        public void LoadAndShowUserListInfo(IList listInfo )
        {
            //清空列表信息
            ClearItems();
            //克隆与显示列表信息
            foreach (UserVO userVo in listInfo)
            {
                UserList_Item item = CloneUserVoInfo();
                //克隆的对象,显示信息
                item.DisplayUserItem(userVo);
                //加入集合保存
                _UserListInfo.Add(item);
            }
            //统计数量
            TxtUseListNumber.text = _UserListInfo.Count.ToString();
        }


        //冻结删除按钮
        public void FreezeDeleteButton()
        {
            btn_Delete.interactable = false;
        }

        //显示删除按钮
        public void ShowDeleteButton()
        {
            btn_Delete.interactable = true;
        }

        #region 私有方法

        //克隆用户信息
        private UserList_Item CloneUserVoInfo()
        {
            UserList_Item item = null;

            //参数检查
            if (UserListItemPrefab!=null)
            {
                item=GameObject.Instantiate(UserListItemPrefab);
                //设置父节点
                item.transform.SetParent(UserListItemPrefab.transform.parent);
                //显示克隆的节点
                item.gameObject.SetActive(true);
                //定义尺寸与相对位置
                item.transform.localScale = Vector3.one;
                item.transform.localPosition = Vector3.zero;
            }

            return item;
        }

        //用户点击“新用户”按钮
        private void ClickBtn_New()
        {
            if(NewUser!=null)
            {
                NewUser.Invoke();
            }
        }

        //用户点击“删除”按钮
        private void ClickBtn_Delete()
        {
            if (DeleteUser != null)
            {
                DeleteUser.Invoke();
            }
        }

        //清空列表信息
        private void ClearItems()
        {
            if (_UserListInfo!=null)
            {
                foreach (UserList_Item item in _UserListInfo)
                {
                    Destroy(item.gameObject);
                }
                _UserListInfo.Clear();
            }
        }

        #endregion

    }
}

UserList_Item

/***
 * 
 *    Title: "App应用" 项目
 *           主题:使用PureMVC 架构App示例应用项目 
 *
 *           视图层: 用户列表信息(一条记录)
 *    Description: 
 *           功能:显示用户列表,一条记录。
 *                  
 *    Date: 2021.4.9
 *    Version: 0.1版本
 *    Modify Recoder: 
 *    
 *   
 */
using System.Collections;
using System.Collections.Generic;
using System.Net.Mime;
using PureMVC.Patterns;
using UnityEngine;
using UnityEngine.UI;

namespace AppByPureMVC
{
    public class UserList_Item:MonoBehaviour
    {
        public Text TxtUserName;
        public Text TxtGender;
        public Text TxtDepartment;
        public Text TxtTel;
        public Text TxtEmail;
        //用户当前选择的数据实体类
        private UserVO _CurrentUserSelectVO;
        //选择控件
        private Toggle _ToggleUserItem;


        void Start()
        {
            //得到Togggle 控件
            _ToggleUserItem = this.GetComponent();
            //注册Toggle 事件
            _ToggleUserItem.onValueChanged.AddListener(OnValuesChangByToggle);
        }


        /// 
        /// 显示用户数据项(一条记录)
        /// 
        /// 
        public void DisplayUserItem(UserVO userData)
        {
            if (userData!=null)
            {
                //保存当前一条信息
                _CurrentUserSelectVO = userData;
                //显示内容
                if (TxtUserName) TxtUserName.text = userData.UserName;
                if (TxtGender)
                {
                    if (userData.Gender)
                    {
                        TxtGender.text = "男";
                    }
                    else
                    {
                        TxtGender.text = "女";
                    }
                }
                if (TxtDepartment) TxtDepartment.text = userData.Department;
                if (TxtTel) TxtTel.text = userData.Telephone;
                if (TxtEmail) TxtEmail.text = userData.Email;
            }
        }

        //用户选择事件
        public void OnValuesChangByToggle(bool isSelect)
        {
            if (isSelect)
            {
                //要把用户选择的信息,发送到“列表信息Mediator类”
                Facade.Instance.SendNotification(ProConsts.Msg_SelectUserInfoByCtrlToUserListMediator, _CurrentUserSelectVO);
            }
        }
    }
}

四、效果展示

你可能感兴趣的:(Unity3D开发技术研究构建框架PureMVC项目之APP界面应用(下篇))