通过 INotifyPropertyChanged 实现观察者模式
2013-09-25 14:49
417 查看
INotifyPropertyChanged它的作用:向客户端发出某一属性值已更改的通知。当属性改变时,它可以通知客户端,并进行界面数据更新.而我们不用写很多复杂的代码来更新界面数据,这样可以做到方法简洁而清晰,松耦合和让方法变得更通用.可用的地方太多了:例如上传进度,实时后台数据变更等地方。它的作用:向客户端发出某一属性值已更改的通知。当属性改变时,它可以通知客户端,并进行界面数据更新.而我们不用写很多复杂的代码来更新界面数据,这样可以做到方法简洁而清晰,松耦合和让方法变得更通用.可用的地方太多了:例如上传进度,实时后台数据变更等地方.目前我发现winform和silverlight都支持,确实是一个强大的接口.
public class DemoCustomer1 : PropertyChangedBase
{
// These fields hold the values for the public properties.
private Guid idValue = Guid.NewGuid();
private string customerNameValue = String.Empty;
private string phoneNumberValue = String.Empty;
// The constructor is private to enforce the factory pattern.
private DemoCustomer1()
{
customerNameValue = "Customer";
phoneNumberValue = "(555)555-5555";
}
// This is the public factory method.
public static DemoCustomer1 CreateNewCustomer()
{
return new DemoCustomer1();
}
// This property represents an ID, suitable
// for use as a primary key in a database.
public Guid ID
{
get
{
return this.idValue;
}
}
public string CustomerName
{
get
{
return this.customerNameValue;
}
set
{
if (value != this.customerNameValue)
{
this.customerNameValue = value;
this.NotifyPropertyChanged(p => p.CustomerName);
//NotifyPropertyChanged("CustomerName");
}
}
}
public string PhoneNumber
{
get
{
return this.phoneNumberValue;
}
set
{
if (value != this.phoneNumberValue)
{
this.phoneNumberValue = value;
this.NotifyPropertyChanged(p => p.PhoneNumber);
//NotifyPropertyChanged("PhoneNumber");
}
}
}
}
/// <summary>
/// 基类
/// </summary>
public class PropertyChangedBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
/// <summary>
/// 扩展方法
/// </summary>
public static class PropertyChangedBaseEx
{
public static void NotifyPropertyChanged<T, TProperty>(this T propertyChangedBase, Expression<Func<T, TProperty>> expression) where T : PropertyChangedBase
{
var memberExpression = expression.Body as MemberExpression;
if (memberExpression != null)
{
string propertyName = memberExpression.Member.Name;
propertyChangedBase.NotifyPropertyChanged(propertyName);
}
else
throw new NotImplementedException();
}
}View CodeView Code
public class DemoCustomer1 : PropertyChangedBase
{
// These fields hold the values for the public properties.
private Guid idValue = Guid.NewGuid();
private string customerNameValue = String.Empty;
private string phoneNumberValue = String.Empty;
// The constructor is private to enforce the factory pattern.
private DemoCustomer1()
{
customerNameValue = "Customer";
phoneNumberValue = "(555)555-5555";
}
// This is the public factory method.
public static DemoCustomer1 CreateNewCustomer()
{
return new DemoCustomer1();
}
// This property represents an ID, suitable
// for use as a primary key in a database.
public Guid ID
{
get
{
return this.idValue;
}
}
public string CustomerName
{
get
{
return this.customerNameValue;
}
set
{
if (value != this.customerNameValue)
{
this.customerNameValue = value;
this.NotifyPropertyChanged(p => p.CustomerName);
//NotifyPropertyChanged("CustomerName");
}
}
}
public string PhoneNumber
{
get
{
return this.phoneNumberValue;
}
set
{
if (value != this.phoneNumberValue)
{
this.phoneNumberValue = value;
this.NotifyPropertyChanged(p => p.PhoneNumber);
//NotifyPropertyChanged("PhoneNumber");
}
}
}
}
/// <summary>
/// 基类
/// </summary>
public class PropertyChangedBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
/// <summary>
/// 扩展方法
/// </summary>
public static class PropertyChangedBaseEx
{
public static void NotifyPropertyChanged<T, TProperty>(this T propertyChangedBase, Expression<Func<T, TProperty>> expression) where T : PropertyChangedBase
{
var memberExpression = expression.Body as MemberExpression;
if (memberExpression != null)
{
string propertyName = memberExpression.Member.Name;
propertyChangedBase.NotifyPropertyChanged(propertyName);
}
else
throw new NotImplementedException();
}
}View CodeView Code
相关文章推荐
- 通过 INotifyPropertyChanged 实现观察者模式
- 通过 INotifyPropertyChanged 实现观察者模式
- 通过 INotifyPropertyChanged 实现观察者模式(转)
- 通过 INotifyPropertyChanged 实现观察者模式
- 通过Observable类和Observer接口实现观察者设计模式
- [Silverlight入门系列]使用MVVM模式(7):ViewModel的INotifyPropertyChanged接口实现
- 通过观察者模式监听媒体库的变化实现APP本地数据自动更新
- [Silverlight入门系列]使用MVVM模式(3):Model的INotifyPropertyChanged接口实现
- android通过观察者模式实现更新UI
- [置顶] 观察者模式及如何通过消息机制(观察者模式)实现模块间解耦
- 通过反射中的 TypeBuilder 来动态实现 INotifyPropertyChanged 接口
- Android Webview 通过反射实现夜间模式
- PHP设计模式之JS实现观察者模式
- 观察者模式--lua实现
- 如何通过Databinding的观察者模式自动刷新RecyclerView.Adapter
- Java 实现观察者(Observer)模式
- 通过F5实现Active/Standby模式主机冗余
- 一个简易的java自带的观察者模式实现
- 大文件 and 多下载任务的封装(二)--观察者模式--实现下载进度的实时更新
- Rhyme/Java 设计模式之原型模式prototype通过clone方法和反序列化实现深复制