• 简单的物流项目实战,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之间的连接器

  • 相关阅读:
    C++学习004-Go To 语句使用
    C++学习003-#define 自定义宏
    C++学习002-C++代码中插入汇编语句
    C++学习001-注释
    Qt 加载Leap motion 手势识别软件 二次开发 hello world
    C++知识点 内存占用问题
    虚拟现实-VR-UE4-编译源代码后,无法运行
    Loadrunner|录制脚本时出现乱码的解决方式
    2月14日学习内容
    构建之法读后感(三)
  • 原文地址:https://www.cnblogs.com/R00R/p/11332141.html
Copyright © 2020-2023  润新知