MVP in Winforms

后端 未结 3 1123
春和景丽
春和景丽 2021-02-06 07:30

I\'m primarily from an ASP.Net background with some MVC. I\'ve also done a little Silverlight and MVVM, however I\'m now about to move into Winforms which I have very little exp

相关标签:
3条回答
  • 2021-02-06 07:57

    I have just checked up how data binding in WinForms uses INotifyPropertyChanged. The data binding through the BindingSource does really support INotifyPropertyChanged if the DataSource object of the BindingSource or model property corresponding to DataMember implements this. You can use M. Fowlers supervising presenter / controller to full extent here: You don't even need a hand-written code, the BindingSource synchronizes the view with the model properties in both directions (model -> view and view -> model), and if the model supports INotifyPropertyChanged then the view will be updated automatically. The code constructs I have used so far:

    1. During view initialization:

      this.bindingSource.DataSource = this.presenter;

    2. Designer-generated code:

      this.textBoxPhone.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSource, "Model.Phone", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));

    The model class:

    public class Customer : INotifyPropertyChanged
    {
        private string _firstName;
        public string FirstName
        {
            get { return _firstName; }
            set
            {
                if (_firstName == value)
                    return;
                _firstName = value;
                NotifyPropertyChanged("FirstName");
            }
        }
    
        private string _lastName;
        public string LastName
        {
            get { return _lastName; }
            set
            {
                if (_lastName == value)
                    return;
                _lastName = value;
                NotifyPropertyChanged("LastName");
            }
        }
    
        private string _company;
        public string Company
        {
            get { return _company; }
            set
            {
                if (_company == value)
                    return;
                _company = value;
                NotifyPropertyChanged("Company");
            }
        }
    
        private string _phone;
        public string Phone
        {
            get { return _phone; }
            set
            {
                if (_phone == value)
                    return;
                _phone = value;
                NotifyPropertyChanged("Phone");
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    The presenter class:

    public class CustomerPresenter
    {
        public CustomerPresenter(Customer model)
        {
            if (model == null)
                throw new ArgumentNullException("model");
    
            this.Model = model;
        }
    
        public Customer Model { get; set; }
    
        public ICustomerView View { private get; set; }
    }
    
    0 讨论(0)
  • 2021-02-06 08:11

    You don't miss anything. MVVM is very suitable with WinForms. Microsoft only encoureges the use of WPF and MVVM pattern with it.

    0 讨论(0)
  • 2021-02-06 08:16

    Try to find examples of Supervising Controller MVP flavor, I use that with WinForms, very successfully I would say. The entities support INotifyPropertyChanged, presenter binds them to the view, and presenter subscribes to the PropertyChanged event so that it knows when view changed something (dirty checking). View is responsible only for binding data, all other functionality is moved to the presenter.

    0 讨论(0)
提交回复
热议问题