我主要有 ASP.Net 背景,懂一些 MVC。我也做了一些 Silverlight 和 MVVM,但是我现在即将转向 Winforms,我对它的经验很少,所以我想知道如何处理 MVP。
典型的 MVP 示例显示演示者设置视图属性(通过某种 IView 接口),例如具体视图将该属性值放入文本框中。可以在 MVP 中使用 INotifyPropertyChanged 来代替这种过时的方法吗?如果可以的话,如何使用?一个非常简单的例子将非常有用!
如果我要创建一个实现 INotifyPropertyChanged 的模型,那么这不是更像 MVVM 吗? (即演示者更新模型,并通过 INotifyPropertyChanged 的魔力更新视图)。然而,我在任何地方读到过有关 MVVM 和 Winforms 的内容,人们都说它不合适。为什么?我的理解是,您可以对任何控件的属性进行数据绑定,那么 Winforms 缺少什么?我试图了解与 WPF 相比 Winforms 中数据绑定的缺点,以及为什么不能使用 MVVM,因为它似乎比 MVP 更容易实现。
提前致谢
安迪.
我刚刚检查了 WinForms 中的数据绑定如何使用INotifyPropertyChanged。
数据绑定通过绑定源确实支持INotifyPropertyChanged if the 数据源的对象绑定源或对应的模型属性数据成员实现这个。您可以在这里充分利用 M. Fowlers 监督演示者/控制者:
您甚至不需要手写代码,绑定源将视图与模型属性在两个方向(模型 -> 视图和视图 -> 模型)同步,并且如果模型支持INotifyPropertyChanged然后视图将自动更新。
到目前为止我使用过的代码结构:
-
在视图初始化期间:
this.bindingSource.DataSource = this.presenter;
-
设计者生成的代码:
this.textBoxPhone.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSource, "Model.Phone", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
模型类:
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));
}
}
演讲者班级:
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; }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)