using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
namespace Demo
{
class Program
{
public static void Main(string[] args)
{
// 创建实例
DemoCustomer demoCustomer = DemoCustomer.CreateNewCustomer();
// 实现事件触发需要处理的事情
demoCustomer.PropertyChanged += new PropertyChangedEventHandler(demoCustomer_PropertyChanged);
// 更新值 引发事件
demoCustomer.PhoneNumber = "100";
// 等待
Console.ReadLine();
}
static void demoCustomer_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
// 获取被更改的属性名
Console.WriteLine(e.PropertyName);
// 获取最新更新的值
Console.WriteLine(((DemoCustomer)sender).PhoneNumber);
}
}
// 实现 INotifyPropertyChanged 接口 进行监听
public class DemoCustomer : INotifyPropertyChanged
{
// 默认的私有属性
private Guid idValue = Guid.NewGuid();
private string customerName = String.Empty;
private string companyNameValue = String.Empty;
private string phoneNumberValue = String.Empty;
/// <summary>
/// 在更改属性时引发的事件。(这个事件将被外露。)
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
// 默认的构造
private DemoCustomer()
{
customerName = "no data";
companyNameValue = "no data";
phoneNumberValue = "no data";
}
// 简单的工厂模式
public static DemoCustomer CreateNewCustomer()
{
return new DemoCustomer();
}
public Guid ID
{
get
{
return this.idValue;
}
}
public string CompanyName
{
get
{
return this.companyNameValue;
}
set
{
if (value != this.companyNameValue)
{
this.companyNameValue = value;
// 当发生改变时,那么就触发事件,传入触发的属性名
NotifyPropertyChanged("CompanyName");
}
}
}
public string PhoneNumber
{
get
{
return this.phoneNumberValue;
}
set
{
if (value != this.phoneNumberValue)
{
this.phoneNumberValue = value;
// 当发生改变时,那么就触发事件,传入触发的属性名
NotifyPropertyChanged("PhoneNumber");
}
}
}
}
}
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
namespace Demo
{
class Program
{
public static void Main(string[] args)
{
// 创建实例
DemoCustomer demoCustomer = DemoCustomer.CreateNewCustomer();
// 实现事件触发需要处理的事情
demoCustomer.PropertyChanged += new PropertyChangedEventHandler(demoCustomer_PropertyChanged);
// 更新值 引发事件
demoCustomer.PhoneNumber = "100";
// 等待
Console.ReadLine();
}
static void demoCustomer_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
// 获取被更改的属性名
Console.WriteLine(e.PropertyName);
// 获取最新更新的值
Console.WriteLine(((DemoCustomer)sender).PhoneNumber);
}
}
// 实现 INotifyPropertyChanged 接口 进行监听
public class DemoCustomer : INotifyPropertyChanged
{
// 默认的私有属性
private Guid idValue = Guid.NewGuid();
private string customerName = String.Empty;
private string companyNameValue = String.Empty;
private string phoneNumberValue = String.Empty;
/// <summary>
/// 在更改属性时引发的事件。(这个事件将被外露。)
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
// 默认的构造
private DemoCustomer()
{
customerName = "no data";
companyNameValue = "no data";
phoneNumberValue = "no data";
}
// 简单的工厂模式
public static DemoCustomer CreateNewCustomer()
{
return new DemoCustomer();
}
public Guid ID
{
get
{
return this.idValue;
}
}
public string CompanyName
{
get
{
return this.companyNameValue;
}
set
{
if (value != this.companyNameValue)
{
this.companyNameValue = value;
// 当发生改变时,那么就触发事件,传入触发的属性名
NotifyPropertyChanged("CompanyName");
}
}
}
public string PhoneNumber
{
get
{
return this.phoneNumberValue;
}
set
{
if (value != this.phoneNumberValue)
{
this.phoneNumberValue = value;
// 当发生改变时,那么就触发事件,传入触发的属性名
NotifyPropertyChanged("PhoneNumber");
}
}
}
}
}