当前位置:网站首页>MVVM of WPF

MVVM of WPF

2022-07-06 07:04:00 zhangxiaomm

Since the contact WPF After that, I kept hearing people mention MVVM. I'm free to tidy up today .

Previously C++(MFC) To c# Of Winform They are all direct editing interfaces , Drag control , Edit the event function of the control . It feels the same , So I write WPF The program is also a routine , Feeling WPF Not yet UI Separate from the logical layer , and Winform Not much difference , Just use Xaml Language replaces drag and drop controls .

Today, I wrote a whole MVVM The program , The feeling is as follows :

  1. mvvm All control events of are controlled by commands command complete , Without controls click event .
  2. Command to connect to viewmodel in , The specific execution function of the command inside , Bound to model The execution function in . amount to viewmodel It's a shell , It is to combine ordinary and specific executive functions with view Events in are bound .
  3. such model The specific method function in does not need to change , It just needs to change viewmodel and view Contents of Li .

such as :model Functional function : Additions and deletions , The interface has two buttons , Corresponding to two commands , These two commands can correspond to model On any method function in . Or another interface , There is another button , It can also correspond to model In the way , At this time, we only need to replace view and viewmodel that will do .

viewmodel Layer code :

 public class MainViewModel : ViewModelBase
    {
        #region Properties and constructors

        private LocalDb localDb;
        
        private ObservableCollection<Student> gridmodelList;
        public ObservableCollection<Student> GridmodelList
        {
            get { return gridmodelList; }
            set { gridmodelList = value; RaisePropertyChanged(); }
        }

        private string search;
        public string Search
        {
            get { return search; }
            set { search = value; RaisePropertyChanged(); }
        }
        public MainViewModel()
        {
            localDb = new LocalDb();
            QueryCommand = new RelayCommand(this.Query);
            ResetCommand = new RelayCommand(this.Reset);
            EditCommand = new RelayCommand<int>(this.Edit);
            DeleteCommand = new RelayCommand<int>(this.Delete);
            AddCommand = new RelayCommand(this.Add);
        }
        #endregion

        // command
        public RelayCommand QueryCommand { get; set; }
        public RelayCommand ResetCommand { get; set; }
        public RelayCommand AddCommand { get; set; }
        public RelayCommand<int> EditCommand { get; set; }
        public RelayCommand<int> DeleteCommand { get; set; }

        public void Query()
        {
            List<Student> students;
            if (string.IsNullOrEmpty(search))
            {
                students = localDb.Query();
            }
            else
            {
                students = localDb.QueryByName(search);
            }

            GridmodelList = new ObservableCollection<Student>();
            if (students != null)
            {
                students.ForEach(t =>
                { GridmodelList.Add(t); });
            }
        }

        public void Reset()
        {
            this.search = string.Empty;
            this.Query();
        }

        public void Edit(int Id)
        {
            var model = localDb.QueryById(Id);
            if (model != null)
            {
                StudentWindow view = new StudentWindow(model);
                var r = view.ShowDialog();
                if (r.Value)
                {
                    var newModel = GridmodelList.FirstOrDefault(t => t.Id == model.Id);
                    if (newModel != null)
                    {
                        newModel.Name = model.Name;
                        newModel.Age = model.Age;
                        newModel.Classes = model.Classes;
                    }
                    this.Query();
                }
            }
        }

        /// <summary>
        /// Delete
        /// </summary>
        /// <param name="Id"></param>
        public void Delete(int Id)
        {
            var model = localDb.QueryById(Id);
            if (model != null)
            {
                var r = MessageBox.Show($" Are you sure you want to delete 【{model.Name}】?", " Tips ", MessageBoxButton.YesNo);
                if (r == MessageBoxResult.Yes)
                {
                    localDb.Remove(Id);
                    this.Query();
                }
            }
        }

        /// <summary>
        /// newly added
        /// </summary>
        public void Add()
        {
            Student model = new Student();
            StudentWindow view = new StudentWindow(model);
            var r = view.ShowDialog();
            if (r.Value)
            {
                model.Id = GridmodelList.Max(t => t.Id) + 1;
                localDb.Add(model);
                this.Query();
            }
        }
    }
}

 

原网站

版权声明
本文为[zhangxiaomm]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060654154830.html