• 通过 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
  • 相关阅读:
    os模块
    sys模块
    psutil模块
    subprocess模块
    time模块
    argparse模块
    tcp端口扫描(python多线程)
    基于http的软件仓库
    SRE思想
    redis哨兵模式
  • 原文地址:https://www.cnblogs.com/zhangpengshou/p/2812495.html
Copyright © 2020-2023  润新知