Winforms中的MVP [英] MVP in Winforms

查看:149
本文介绍了Winforms中的MVP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我主要是从一个ASP.Net的背景与一些MVC。我也做了一些Silverlight和MVVM,但是现在我正要进入Winforms,我很少有经验,所以我想知道如何处理MVP。


$ b $典型的MVP示例显示演示者设置一个视图属性(通过某种IView界面),具体的视图将该属性值放入一个文本框中。而不是这种古老的方法,可以在MVP中使用INotifyPropertyChanged,如果是这样呢?一个非常快的例子将是非常有用的!



如果我要创建一个实现INotifyPropertyChanged的模型,那么这不是更像MVVM吗? (即,演示者更新模型,并通过INotifyPropertyChanged的魔力更新视图)。然而,我已经阅读过关于MVVM和Winforms的各个方面,有人说这是不合适的。为什么?我的理解是,您可以对任何控件的属性进行数据绑定,那么Winforms缺少什么?我想了解Winforms与WPF相比的数据绑定的缺点,为什么不能使用MVVM,因为它似乎比MVP更简单实现。



谢谢提前
Andy。

解决方案

我刚刚检查了WinForms中的数据绑定如何使用 INotifyPropertyChanged <强>。
通过 BindingSource 绑定的数据确实支持 INotifyPropertyChanged ,如果 BindingSource DataSource 对象或对应于 DataMember 的model属性实现了这一点。您可以使用M. Fowlers全面监督演示者/控制器:
您甚至不需要手写代码, BindingSource 将视图与两个方向的模型属性同步(模型 - >视图和视图 - >模型),如果模型支持 INotifyPropertyChanged ,则视图将自动更新。
到目前为止我已经使用的代码结构:


  1. 在视图初始化期间:



    this.bindingSource.DataSource = this.presenter;


  2. 设计者生成的代码:



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


模型类:

 code> public class Customer:INotifyPropertyChanged 
{
private string _firstName;
public string FirstName
{
get {return _firstName; }
set
{
if(_firstName == value)
return;
_firstName = value;
NotifyPropertyChanged(FirstName);
}
}

私有字符串_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);
}
}

私人字符串_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));
}
}

演示者类:

  public class CustomerPresenter 
{
public CustomerPresenter(Customer model)
{
if(model == null )
throw new ArgumentNullException(model);

this.Model = model;
}

public客户模型{get;组; }

public ICustomerView View {private get;组; }
}


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 experience of, so I'm wondering how to tackle MVP.

Typical MVP samples show the presenter setting a view property (via some kind of IView interface), with the concrete view putting that property value into a textbox for example. Instead of this archaic approach, can one utilise INotifyPropertyChanged in MVP, and if so how? A very quick example would be really useful!

If I was to create a model that implemented INotifyPropertyChanged then isn't this more like MVVM? (i.e. the presenter updates the model, and via the magic of INotifyPropertyChanged the view gets updated). Yet everywhere I've read about MVVM and Winforms, people say it isn't suitable. Why? My understanding is that you can databind just about any control's property, so what's Winforms missing? I'm trying to understand the shortcomings of databinding in Winforms compared to WPF, and why MVVM can't be used, as it seems simpler to implement than MVP.

Thanks in advance Andy.

解决方案

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; }
}

这篇关于Winforms中的MVP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆