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

Posted r00r

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单的物流项目实战,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之间的连接器

以上是关于简单的物流项目实战,WPF的MVVM设计模式的主要内容,如果未能解决你的问题,请参考以下文章

WPF实战案例-MVVM模式下在Xaml中弹出窗体

C# WPF MVVM项目实战(进阶②)

C# WPF MVVM项目实战(进阶②)

C# WPF MVVM项目实战(进阶①)

2022年终结版WPF项目实战合集发布

Prism完成的一个WPF项目