简单的物流项目实战,WPF的MVVM设计模式(四)

接下来写ViewModels

创建运单的ViewModel类

    public class CreateExpressWindowViewModel: NotificationObject
    {
        private string province;

        public string Province
        {
            get
            {
                return province;
            }
            set
            {
                province = value;
                this.RaisePropertyChanged("Province");
            }
        }

        private string city;

        public string City
        {
            get
            {
                return city;
            }
            set
            {
                city = value;
                this.RaisePropertyChanged("City");
            }
        }

        private string area;

        public string Area
        {
            get
            {
                return area;
            }
            set
            {
                area = value;
                this.RaisePropertyChanged("Area");
            }
        }

        private string street;

        public string Street
        {
            get
            {
                return street;
            }
            set
            {
                street = value;
                this.RaisePropertyChanged("Street");
            }
        }

        private string username;

        public string Username
        {
            get
            {
                return username;
            }
            set
            {
                username = value;
                this.RaisePropertyChanged("Username");
            }
        }

        private string phonenumber;

        public string Phonenumber
        {
            get
            {
                return phonenumber;
            }
            set
            {
                phonenumber = value;
                this.RaisePropertyChanged("Phonenumber");
            }
        }

        private string createtime;

        public string Createtime
        {
            get
            {
                return createtime;
            }
            set
            {
                createtime = value;
                this.RaisePropertyChanged("Createtime");
            }
        }

        public DelegateCommand CreateExpressCommand { get; set; }

        public CreateExpressWindowViewModel()
        {
            Createtime = DateTime.Now.ToShortDateString();
            CreateExpressCommand = new DelegateCommand(new Action(InsertExpress));
        }

        private void InsertExpress()
        {
            //Random rd = new Random();
            //int num = rd.Next(10000000, 99999999);
            
            InsertExpressService insertExpressService = new InsertExpressService();
            Express express = new Express();
            insertExpressService.InsertExpress(Province,City,Area,Street,Username,Phonenumber,Createtime);
            MessageBox.Show("成功下单!");
            ExpressWindow expressWindow = new ExpressWindow();
           
            ReadExpressWindow readExpressWindow = new ReadExpressWindow();
            readExpressWindow.Show();
            expressWindow.Close();
        }
    }

public class GetExpressWindow : NotificationObject
    {
        private string province;

        public string Province
        {
            get
            {
                return province;
            }
            set
            {
                province = value;
                this.RaisePropertyChanged("Province");
            }
        }

        private string city;

        public string City
        {
            get
            {
                return city;
            }
            set
            {
                city = value;
                this.RaisePropertyChanged("City");
            }
        }

        private string area;

        public string Area
        {
            get
            {
                return area;
            }
            set
            {
                area = value;
                this.RaisePropertyChanged("Area");
            }
        }

        private string street;

        public string Street
        {
            get
            {
                return street;
            }
            set
            {
                street = value;
                this.RaisePropertyChanged("Street");
            }
        }

        private string username;

        public string Username
        {
            get
            {
                return username;
            }
            set
            {
                username = value;
                this.RaisePropertyChanged("Username");
            }
        }

        private string phonenumber;

        public string Phonenumber
        {
            get
            {
                return phonenumber;
            }
            set
            {
                phonenumber = value;
                this.RaisePropertyChanged("Phonenumber");
            }
        }

        private string createtime;

        public string Createtime
        {
            get
            {
                return createtime;
            }
            set
            {
                createtime = value;
                this.RaisePropertyChanged("Createtime");
            }
        }
        public DelegateCommand GetExpressCommand { get; set; }
        public GetExpressWindow()
        {
            GetExpressCommand = new DelegateCommand(new Action(GetExpress));
        }

        private void GetExpress()
        {
            GetExpressService getExpressService = new GetExpressService();
            Express express = new Express();
            express = getExpressService.GetExpressData();
            Province = express.Province;
            City = express.City;
            Area = express.Area;
            Street = express.Street;
            Username = express.UserName;
            Phonenumber = express.PhoneNumber;
            Createtime = express.CteateTime;
        }
    }

    public class RegisteredWindowViewModel : NotificationObject
    {
        

        private string userAccount;

        public string UserAccount
        {
            get
            {
                return userAccount;
            }
            set
            {
                userAccount = value;
                this.RaisePropertyChanged("UserAccount");
            }
        }

        private string password;

        public string Password
        {
            get
            {
                return password;
            }
            set
            {
                password = value;
                this.RaisePropertyChanged("Password");
            }
        }
        
        public DelegateCommand InsertCommand { get; set; }
        public RegisteredWindowViewModel()
        {
            InsertCommand = new DelegateCommand(new Action(InsertUserData));
        }

        private void InsertUserData()
        {
            InsertRegisteredService insertRegisteredService = new InsertRegisteredService();
            User user = new User();
            user.UserAccount = UserAccount;
            user.Password = Password;
            user = insertRegisteredService.InsertUser(UserAccount, Password);
            MessageBox.Show("注册成功!");
        }
    }

   public class UserViewModel: NotificationObject
    {
        /// <summary>
        /// 判断登录用户是否存在
        /// </summary>
        private bool exist;

        public bool Exist
        {
            get
            {
                return exist;
            }
            set
            {
                exist = value;
                this.RaisePropertyChanged("Exist");
            }
        }

    }

    public class UserWindowViewModel : NotificationObject
    {

        //var logger = NLog.LogManager.GetCurrentClassLogger();
        //logger.Info("Hello World");

        //var a = LogManager.GetCurrentClassLogger();
        private string username;

        public string Username
        {
            get
            {
                return username;
            }
            set
            {
                username = value;
                this.RaisePropertyChanged("Username");
            }
        }

        private string userpassword;

        public string Userpassword
        {
            get
            {
                return userpassword;
            }
            set
            {
                userpassword = value;
                this.RaisePropertyChanged("Userpassword");
            }
        }



        public DelegateCommand GetCommand { get; set; }

        public UserWindowViewModel()
        {
            GetCommand = new DelegateCommand(new Action(GetUser));
        }
        private void GetUser()
        {
            GetUserService getUserServicelist = new GetUserService();
            List<User> userslist = getUserServicelist.GetAllUser();

            List<User> usernamecount = userslist.Where(x=>x.UserAccount==Username).ToList();
            List<User> passwordcount = userslist.Where(x=>x.Password==Userpassword).ToList();

            ///方法二
            for (int i = 0; i < usernamecount.Count; i++)
            {
                if (usernamecount[i].UserAccount==Username&&usernamecount[i].Password==Userpassword)
                {
                    MessageBox.Show("登录成功!");
                    ExpressWindow expressWindow = new ExpressWindow();
                    expressWindow.ShowDialog();
                    return;
                }
                else
                {
                    MessageBox.Show("账号不存在或密码错误!");
                    return;
                }
            }

            ///方法一
            //if (usernamecount.Count > 0 && passwordcount.Count > 0)
            //{
            //    MessageBox.Show("登录成功!");
            //    return;
            //}
            //else
            //{
            //    MessageBox.Show("登录失败!");
            //    return;
            //}

        }
    }

以上分别为创建运单,查看运单,以及注册,以及登录的ViewModels,作为Views和Models之间的连接器

posted @ 2019-08-10 16:46  Lexan  阅读(695)  评论(0编辑  收藏  举报