INotifyPropertyChanged接口的详细说明

移动开发
在windows phone开发8.1:数据绑定中,我们了解了数据绑定的基本知识.今后几篇文章会继续深入了解数据绑定.今天我们来看在数据绑定中十分重要的INotifyPropertyChanged接口的实现. 何时实现INotifyPropertyChanged接口

在windows phone开发8.1:数据绑定中,我们了解了数据绑定的基本知识.今后几篇文章会继续深入了解数据绑定.今天我们来看在数据绑定中十分重要的INotifyPropertyChanged接口的实现.
何时实现INotifyPropertyChanged接口

官方解释:INotifyPropertyChanged 接口用于向客户端(通常是执行绑定的客户端)发出某一属性值已更改的通知。官方解释的很模糊,估计是个人看了都不知道到底什么时候需要实现INotifyPropertyChanged接口.小梦通过实际测试给出明确结论:

首先:OneTime模式:毫无意义,因为它的绑定只有初始时候绑定一次,根本谈不上改变!自然也就谈不上实现INotifyPropertyChanged接口.

然后是OneWay模式:我们知道OneWay模式的含义是:绑定源的每一次变化都会通知绑定目标,但是绑定目标的改变不会改变绑定源.当绑定源的数据实体类没有实现INotifyPropertyChanged接口时,当我们改变了数据源,我们会发现绑定目标的UI上的相应的数据不会立即变化.所以这时候就需要我们来实现INotifyPropertyChanged接口.

***是TwoWay模式:在TwoWay模式下,当绑定源的数据实体类没有实现INotifyPropertyChanged接口时,我们发现.控件的更改会让数据源立即发改变,但是改变数据源,绑定目标控件却不会立即发生改变!所以当我们需要数据源改变时相对应的UI立即改变时,就需要实现INotifyPropertyChanged接口.

总之:就是当数据源改变并需要UI立即改变时我们需要实现INotifyPropertyChanged接口.

我们可以通过这个示例来明确的体会这一点:

<StackPanel> 
 
       <TextBox  Header="编号" Text="{Binding ID,Mode=OneTime}" Name="tbxID"  ></TextBox> 
 
        <TextBox Header="书名" Text="{Binding Title,Mode=OneWay}" Name="tbxTitle" ></TextBox> 
 
       <TextBox  Header="价格" Text="{Binding Price,Mode=TwoWay}" Name="tbxPrice" ></TextBox> 
 
     <Button Content="通过数据源修改控件的值"  Click="Button_Click"></Button> 
 
         <Button Content="直接修改控件的值"     Click="Button_Click_1" /> 
 
        <Button Content="通过控件修改数据源的值"   Click="Button_Click_2" /> 
 
       </StackPanel> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

后台代码

namespace INotifyPropertyChangedDEMO 

    /// <summary> 
    /// 可用于自身或导航至 Frame 内部的空白页。 
    /// </summary> 
 
    public sealed partial class MainPage : Page 
    { 
        Book book = new Book(); 
        public MainPage() 
        { 
            this.InitializeComponent(); 
 
            this.NavigationCacheMode = NavigationCacheMode.Required; 
            book.ID = 0
            book.Title = "ASP.NET 开发手册"
            book.Price = 40
            st.DataContext = book; 
        } 
  private void Button_Click(object sender, RoutedEventArgs e)//通过修改数据源修改控件的值 
        { 
            book.ID = 100
            book.Price = 50
            book.Title = "SL开发手册"
        } 
 
        private async void Button_Click_1(object sender, RoutedEventArgs e)//显示数据源的值 
        { 
            await new MessageDialog(book.ID.ToString() + " " + book.Title.ToString() + " " + book.Price.ToString()).ShowAsync(); 
        } 
 
        public class Book : INotifyPropertyChanged 
//INotifyPropertChanged 接口定义了一个当属性值更改时执行的事件,事件名称为PropertyChanged。 
     //这个是在继承这个接口的类必须要实现的事件 
 
        { 
            private int _id; 
            public int ID 
            { 
                get { return _id; } 
                set 
                { 
                    _id = value; 
                    //NotifyPropertyChange("ID"); 
                } 
            } 
            private string _title; 
            public string Title 
            { 
                get { return _title; } 
                set 
                { 
                    _title = value; 
                    //NotifyPropertyChange("Title"); 
                } 
            } 
            private double _price; 
            public double Price 
            { 
                get { return _price; } 
                set 
                { 
                    _price = value; 
                    //NotifyPropertyChange("Price"); 
                } 
            } 
            public event PropertyChangedEventHandler PropertyChanged; 
            //PropertyChangedEventArgs类型,这个类用于传递更改值的属性的名称,实现向客户端已经更改的属性发送更改通知。属性的名称为字符串类型。 
            private void NotifyPropertyChange(string propertyName) 
            { 
                if (PropertyChanged != null
                { 
                    //根据PropertyChanged事件的委托类,实现PropertyChanged事件: 
                    PropertyChanged(thisnew PropertyChangedEventArgs(propertyName)); 
                } 
            } 
        } 
    } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.

大家运行这个示例可以明显体会INotifyPropertyChanged接口的作用.
如何实现INotifyPropertyChanged接口

上面示例的INotifyPropertyChanged接口的实现方式是最常见和最普遍的.

我们可以利用CallerMemberNameAttribute特性来简化一下,这个特性可以根据调用方来决定传入哪个属性的名字.:

protected void OnPropertyChanged([CallerMemberName] string propertyName = null
        { 
            var eventHandler = this.PropertyChanged; 
            if (eventHandler != null
                eventHandler(thisnew PropertyChangedEventArgs(propertyName)); 
        } 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

这样我们在调用时可以这样调用:

NotifyPropertyChange("ID") 改为:OnPropertyChanged();

INotifyPropertyChanged接口的***实现方式:

这个所谓的***实现方式 是channel 9的视频中说的,实现方式如下:

public class ModelBase : INotifyPropertyChanged 
    { 
        public event PropertyChangedEventHandler PropertyChanged; 
        protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null
        { 
            if (object.Equals(storage, value)) return false
            storage = value; 
            this.OnPropertyChanged(propertyName); 
            return true
        } 
 
        protected void OnPropertyChanged([CallerMemberName] string propertyName = null
        { 
            var eventHandler = this.PropertyChanged; 
            if (eventHandler != null
                eventHandler(thisnew PropertyChangedEventArgs(propertyName)); 
        } 
    } 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

相应的调用方式进一步简化:

private string name; 
 
       public string Name 
       { 
           get { return name; } 
           set 
           { this.SetProperty(ref this.name, value); } 
       } 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

本文链接:http://www.cnblogs.com/bcmeng/p/3966931.html

责任编辑:chenqingxiang 来源: cnblogs
相关推荐

2009-12-28 09:26:09

ADO对象

2009-12-08 17:34:25

WCF的配置

2010-10-13 14:28:09

mysql日志文件

2010-02-22 17:54:07

Python工具

2009-12-07 18:06:46

WCF框架

2010-07-21 13:17:52

Perl文件读写

2009-12-08 09:00:14

WCF线程

2010-03-09 10:11:34

Linux挂载命令

2010-02-22 16:26:21

Python编辑

2010-02-04 14:41:52

Android菜单类型

2010-01-08 15:37:59

JSON数据

2010-02-05 18:09:12

Android

2009-12-11 15:31:17

Visual Stud

2009-12-10 17:54:34

Visual Stud

2010-02-06 15:53:55

2009-11-04 10:16:01

2010-06-11 16:46:06

openSUSE Fl

2010-02-06 17:43:51

Android应用

2009-12-07 13:12:18

WFC端口

2010-03-03 18:17:01

Android手机服务
点赞
收藏

51CTO技术栈公众号