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

INotifyPropertyChanged接口的詳細(xì)說明

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

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

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

首先:OneTime模式:毫無意義,因?yàn)樗慕壎ㄖ挥谐跏紩r(shí)候綁定一次,根本談不上改變!自然也就談不上實(shí)現(xiàn)INotifyPropertyChanged接口.

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

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

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

我們可以通過這個(gè)示例來明確的體會(huì)這一點(diǎn):

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

后臺(tái)代碼

  1. namespace INotifyPropertyChangedDEMO 
  2.     /// <summary> 
  3.     /// 可用于自身或?qū)Ш街?nbsp;Frame 內(nèi)部的空白頁(yè)。 
  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ā)手冊(cè)"
  16.             book.Price = 40
  17.             st.DataContext = book; 
  18.         } 
  19.   private void Button_Click(object sender, RoutedEventArgs e)//通過修改數(shù)據(jù)源修改控件的值 
  20.         { 
  21.             book.ID = 100
  22.             book.Price = 50
  23.             book.Title = "SL開發(fā)手冊(cè)"
  24.         } 
  25.  
  26.         private async void Button_Click_1(object sender, RoutedEventArgs e)//顯示數(shù)據(jù)源的值 
  27.         { 
  28.             await new MessageDialog(book.ID.ToString() + " " + book.Title.ToString() + " " + book.Price.ToString()).ShowAsync(); 
  29.         } 
  30.  
  31.         public class Book : INotifyPropertyChanged 
  32. //INotifyPropertChanged 接口定義了一個(gè)當(dāng)屬性值更改時(shí)執(zhí)行的事件,事件名稱為PropertyChanged。 
  33.      //這個(gè)是在繼承這個(gè)接口的類必須要實(shí)現(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類型,這個(gè)類用于傳遞更改值的屬性的名稱,實(shí)現(xiàn)向客戶端已經(jīng)更改的屬性發(fā)送更改通知。屬性的名稱為字符串類型。 
  68.             private void NotifyPropertyChange(string propertyName) 
  69.             { 
  70.                 if (PropertyChanged != null
  71.                 { 
  72.                     //根據(jù)PropertyChanged事件的委托類,實(shí)現(xiàn)PropertyChanged事件: 
  73.                     PropertyChanged(thisnew PropertyChangedEventArgs(propertyName)); 
  74.                 } 
  75.             } 
  76.         } 
  77.     } 

大家運(yùn)行這個(gè)示例可以明顯體會(huì)INotifyPropertyChanged接口的作用.
如何實(shí)現(xiàn)INotifyPropertyChanged接口

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

我們可以利用CallerMemberNameAttribute特性來簡(jiǎn)化一下,這個(gè)特性可以根據(jù)調(diào)用方來決定傳入哪個(gè)屬性的名字.:

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

這樣我們?cè)谡{(diào)用時(shí)可以這樣調(diào)用:

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

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

這個(gè)所謂的***實(shí)現(xiàn)方式 是channel 9的視頻中說的,實(shí)現(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.     } 

相應(yīng)的調(diào)用方式進(jìn)一步簡(jiǎn)化:

  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

責(zé)任編輯:chenqingxiang 來源: cnblogs
相關(guān)推薦

2009-12-28 09:26:09

ADO對(duì)象

2009-12-08 17:34:25

WCF的配置

2010-10-13 14:28:09

mysql日志文件

2009-12-08 09:00:14

WCF線程

2010-02-22 17:54:07

Python工具

2009-12-07 18:06:46

WCF框架

2010-03-09 10:11:34

Linux掛載命令

2010-07-21 13:17:52

Perl文件讀寫

2010-02-04 14:41:52

Android菜單類型

2010-02-22 16:26:21

Python編輯

2010-01-08 15:37:59

JSON數(shù)據(jù)

2010-02-05 18:09:12

Android

2010-02-06 17:43:51

Android應(yīng)用

2010-06-11 16:46:06

openSUSE Fl

2009-12-10 17:54:34

Visual Stud

2010-02-06 15:53:55

2009-12-11 15:31:17

Visual Stud

2010-03-03 18:17:01

Android手機(jī)服務(wù)

2009-11-04 10:16:01

2009-12-07 13:12:18

WFC端口
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)