偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

INotifyPropertyChanged接口的詳細說明

移動開發(fā)
在windows phone開發(fā)8.1:數(shù)據綁定中,我們了解了數(shù)據綁定的基本知識.今后幾篇文章會繼續(xù)深入了解數(shù)據綁定.今天我們來看在數(shù)據綁定中十分重要的INotifyPropertyChanged接口的實現(xiàn). 何時實現(xiàn)INotifyPropertyChanged接口

在windows phone開發(fā)8.1:數(shù)據綁定中,我們了解了數(shù)據綁定的基本知識.今后幾篇文章會繼續(xù)深入了解數(shù)據綁定.今天我們來看在數(shù)據綁定中十分重要的INotifyPropertyChanged接口的實現(xiàn).
何時實現(xiàn)INotifyPropertyChanged接口

官方解釋:INotifyPropertyChanged 接口用于向客戶端(通常是執(zhí)行綁定的客戶端)發(fā)出某一屬性值已更改的通知。官方解釋的很模糊,估計是個人看了都不知道到底什么時候需要實現(xiàn)INotifyPropertyChanged接口.小夢通過實際測試給出明確結論:

首先:OneTime模式:毫無意義,因為它的綁定只有初始時候綁定一次,根本談不上改變!自然也就談不上實現(xiàn)INotifyPropertyChanged接口.

然后是OneWay模式:我們知道OneWay模式的含義是:綁定源的每一次變化都會通知綁定目標,但是綁定目標的改變不會改變綁定源.當綁定源的數(shù)據實體類沒有實現(xiàn)INotifyPropertyChanged接口時,當我們改變了數(shù)據源,我們會發(fā)現(xiàn)綁定目標的UI上的相應的數(shù)據不會立即變化.所以這時候就需要我們來實現(xiàn)INotifyPropertyChanged接口.

***是TwoWay模式:在TwoWay模式下,當綁定源的數(shù)據實體類沒有實現(xiàn)INotifyPropertyChanged接口時,我們發(fā)現(xiàn).控件的更改會讓數(shù)據源立即發(fā)改變,但是改變數(shù)據源,綁定目標控件卻不會立即發(fā)生改變!所以當我們需要數(shù)據源改變時相對應的UI立即改變時,就需要實現(xiàn)INotifyPropertyChanged接口.

總之:就是當數(shù)據源改變并需要UI立即改變時我們需要實現(xiàn)INotifyPropertyChanged接口.

我們可以通過這個示例來明確的體會這一點:

  1. <StackPanel> 
  2.  
  3.        <TextBox  Header="編號" Text="{Binding ID,Mode=OneTime}" Name="tbxID"  ></TextBox> 
  4.  
  5.         <TextBox Header="書名" Text="{Binding Title,Mode=OneWay}" Name="tbxTitle" ></TextBox> 
  6.  
  7.        <TextBox  Header="價格" Text="{Binding Price,Mode=TwoWay}" Name="tbxPrice" ></TextBox> 
  8.  
  9.      <Button Content="通過數(shù)據源修改控件的值"  Click="Button_Click"></Button> 
  10.  
  11.          <Button Content="直接修改控件的值"     Click="Button_Click_1" /> 
  12.  
  13.         <Button Content="通過控件修改數(shù)據源的值"   Click="Button_Click_2" /> 
  14.  
  15.        </StackPanel> 

后臺代碼

  1. namespace INotifyPropertyChangedDEMO 
  2.     /// <summary> 
  3.     /// 可用于自身或導航至 Frame 內部的空白頁。 
  4.     /// </summary> 
  5.  
  6.     public sealed partial class MainPage : Page 
  7.     { 
  8.         Book book = new Book(); 
  9.         public MainPage() 
  10.         { 
  11.             this.InitializeComponent(); 
  12.  
  13.             this.NavigationCacheMode = NavigationCacheMode.Required; 
  14.             book.ID = 0
  15.             book.Title = "ASP.NET 開發(fā)手冊"
  16.             book.Price = 40
  17.             st.DataContext = book; 
  18.         } 
  19.   private void Button_Click(object sender, RoutedEventArgs e)//通過修改數(shù)據源修改控件的值 
  20.         { 
  21.             book.ID = 100
  22.             book.Price = 50
  23.             book.Title = "SL開發(fā)手冊"
  24.         } 
  25.  
  26.         private async void Button_Click_1(object sender, RoutedEventArgs e)//顯示數(shù)據源的值 
  27.         { 
  28.             await new MessageDialog(book.ID.ToString() + " " + book.Title.ToString() + " " + book.Price.ToString()).ShowAsync(); 
  29.         } 
  30.  
  31.         public class Book : INotifyPropertyChanged 
  32. //INotifyPropertChanged 接口定義了一個當屬性值更改時執(zhí)行的事件,事件名稱為PropertyChanged。 
  33.      //這個是在繼承這個接口的類必須要實現(xiàn)的事件 
  34.  
  35.         { 
  36.             private int _id; 
  37.             public int ID 
  38.             { 
  39.                 get { return _id; } 
  40.                 set 
  41.                 { 
  42.                     _id = value; 
  43.                     //NotifyPropertyChange("ID"); 
  44.                 } 
  45.             } 
  46.             private string _title; 
  47.             public string Title 
  48.             { 
  49.                 get { return _title; } 
  50.                 set 
  51.                 { 
  52.                     _title = value; 
  53.                     //NotifyPropertyChange("Title"); 
  54.                 } 
  55.             } 
  56.             private double _price; 
  57.             public double Price 
  58.             { 
  59.                 get { return _price; } 
  60.                 set 
  61.                 { 
  62.                     _price = value; 
  63.                     //NotifyPropertyChange("Price"); 
  64.                 } 
  65.             } 
  66.             public event PropertyChangedEventHandler PropertyChanged; 
  67.             //PropertyChangedEventArgs類型,這個類用于傳遞更改值的屬性的名稱,實現(xiàn)向客戶端已經更改的屬性發(fā)送更改通知。屬性的名稱為字符串類型。 
  68.             private void NotifyPropertyChange(string propertyName) 
  69.             { 
  70.                 if (PropertyChanged != null
  71.                 { 
  72.                     //根據PropertyChanged事件的委托類,實現(xiàn)PropertyChanged事件: 
  73.                     PropertyChanged(thisnew PropertyChangedEventArgs(propertyName)); 
  74.                 } 
  75.             } 
  76.         } 
  77.     } 

大家運行這個示例可以明顯體會INotifyPropertyChanged接口的作用.
如何實現(xiàn)INotifyPropertyChanged接口

上面示例的INotifyPropertyChanged接口的實現(xiàn)方式是最常見和最普遍的.

我們可以利用CallerMemberNameAttribute特性來簡化一下,這個特性可以根據調用方來決定傳入哪個屬性的名字.:

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

這樣我們在調用時可以這樣調用:

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

INotifyPropertyChanged接口的***實現(xiàn)方式:

這個所謂的***實現(xiàn)方式 是channel 9的視頻中說的,實現(xiàn)方式如下:

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

相應的調用方式進一步簡化:

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

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

責任編輯:chenqingxiang 來源: cnblogs
相關推薦

2009-12-28 09:26:09

ADO對象

2010-10-13 14:28:09

mysql日志文件

2009-12-08 17:34:25

WCF的配置

2010-02-04 14:41:52

Android菜單類型

2010-02-22 16:26:21

Python編輯

2009-12-07 18:06:46

WCF框架

2010-02-22 17:54:07

Python工具

2009-12-08 09:00:14

WCF線程

2010-03-09 10:11:34

Linux掛載命令

2010-07-21 13:17:52

Perl文件讀寫

2010-01-08 15:37:59

JSON數(shù)據

2010-02-05 18:09:12

Android

2009-12-14 09:47:39

.net2008

2009-12-14 17:57:21

NET 源碼

2009-12-28 09:19:21

ADO屬性

2010-02-04 15:20:50

Android SDK

2010-02-22 16:40:22

Python解釋器

2010-03-05 14:16:38

Android設計平臺

2010-02-04 09:55:46

Android DEX

2009-12-10 16:17:23

Visual Stud
點贊
收藏

51CTO技術棧公眾號