INotifyPropertyChanged强大!

它的作用:向客户端发出某一属性值已更改的通知。

当属性改变时,它可以通知客户端,并进行界面数据更新.而我们不用写很多复杂的代码来更新界面数据,这样可以做到方法简洁而清晰,松耦合和让方法变得更通用.可用的地方太多了:例如上传进度,实时后台数据变更等地方.目前我发现winform和silverlight都支持,确实是一个强大的接口.

下面是winfomr实现接口的方法:

using System;
using System.ComponentModel;

namespace TaskVision.Components
{
    public class MemberModel : INotifyPropertyChanged
    {
        private int _id = 0;
        private int _userID = 0;
        private string _cnName = string.Empty;
        private string _petName = string.Empty;
        private string _enName = string.Empty;
        private bool _isUpload = false;
        private string _progress = "0/0";
        private int _finishCount = 0;
        private int _allCount = 0;

        public string Progress {
            get
            {
                return this._progress;
            }
            set
            {
                if (value != this._progress)
                {
                    this._progress = value;
                    NotifyPropertyChanged("Progress");

                }
            }
        }
        public int ID
        {
            get
            {
                return this._id;
            }
            set
            {
                this._id = value;
            }
        }

        public int UserID
        {
            get
            {
                return this._userID;
            }
            set
            {
                this._userID = value;
            }
        }

        public string CnName
        {
            get
            {
                return this._cnName;
            }
            set
            {
                this._cnName = value;
            }
        }
        public string PetName
        {
            get
            {
                return this._petName;
            }
            set
            {
                this._petName = value;
            }
        }
        public string EnName
        {
            get
            {
                return this._enName;
            }
            set
            {
                this._enName = value;
            }
        }
        public bool IsUpload
        {
            get
            {
                return this._isUpload;
            }
            set
            {
                this._isUpload = value;
            }
        }
        public int FinishCount
        {
            get
            {
                return this._finishCount;
            }
            set
            {
                this._finishCount = value;
            }
        }
        public int AllCount
        {
            get
            {
                return this._allCount;
            }
            set
            {
                this._allCount = value;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    }
}

你可能感兴趣的:(property)