通过 INotifyPropertyChanged 实现观察者模式(转)

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( thisnew 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 ");
                }
            }
        }
    }
}
转自: http://www.cnblogs.com/scarbean/archive/2010/11/05/1870056.html

转载于:https://www.cnblogs.com/zhangpengshou/archive/2012/12/11/2812495.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值