zoukankan      html  css  js  c++  java
  • 通过 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
  • 相关阅读:
    摄影基础知识(二)
    std::bind
    摄影网站汇总
    std::function
    常用路径说明
    摄影基础知识(一)
    JavaScript 箭头函数:适用与不适用场景
    软帝学院:Java实现的5大排序算法
    软帝学院:用Java编写计算器,代码展示!
    windows环境下运行java的脚本
  • 原文地址:https://www.cnblogs.com/zhangpengshou/p/2812495.html
Copyright © 2011-2022 走看看