INotifyPropertyChanged接口的詳細(xì)說明
在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):
- <StackPanel>
- <TextBox Header="編號(hào)" Text="{Binding ID,Mode=OneTime}" Name="tbxID" ></TextBox>
- <TextBox Header="書名" Text="{Binding Title,Mode=OneWay}" Name="tbxTitle" ></TextBox>
- <TextBox Header="價(jià)格" Text="{Binding Price,Mode=TwoWay}" Name="tbxPrice" ></TextBox>
- <Button Content="通過數(shù)據(jù)源修改控件的值" Click="Button_Click"></Button>
- <Button Content="直接修改控件的值" Click="Button_Click_1" />
- <Button Content="通過控件修改數(shù)據(jù)源的值" Click="Button_Click_2" />
- </StackPanel>
后臺(tái)代碼
- namespace INotifyPropertyChangedDEMO
- {
- /// <summary>
- /// 可用于自身或?qū)Ш街?nbsp;Frame 內(nèi)部的空白頁(yè)。
- /// </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 開發(fā)手冊(cè)";
- book.Price = 40;
- st.DataContext = book;
- }
- private void Button_Click(object sender, RoutedEventArgs e)//通過修改數(shù)據(jù)源修改控件的值
- {
- book.ID = 100;
- book.Price = 50;
- book.Title = "SL開發(fā)手冊(cè)";
- }
- private async void Button_Click_1(object sender, RoutedEventArgs e)//顯示數(shù)據(jù)源的值
- {
- await new MessageDialog(book.ID.ToString() + " " + book.Title.ToString() + " " + book.Price.ToString()).ShowAsync();
- }
- public class Book : INotifyPropertyChanged
- //INotifyPropertChanged 接口定義了一個(gè)當(dāng)屬性值更改時(shí)執(zhí)行的事件,事件名稱為PropertyChanged。
- //這個(gè)是在繼承這個(gè)接口的類必須要實(shí)現(xiàn)的事件
- {
- 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類型,這個(gè)類用于傳遞更改值的屬性的名稱,實(shí)現(xiàn)向客戶端已經(jīng)更改的屬性發(fā)送更改通知。屬性的名稱為字符串類型。
- private void NotifyPropertyChange(string propertyName)
- {
- if (PropertyChanged != null)
- {
- //根據(jù)PropertyChanged事件的委托類,實(shí)現(xiàn)PropertyChanged事件:
- PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- }
- }
- }
大家運(yùn)行這個(gè)示例可以明顯體會(huì)INotifyPropertyChanged接口的作用.
如何實(shí)現(xiàn)INotifyPropertyChanged接口
上面示例的INotifyPropertyChanged接口的實(shí)現(xiàn)方式是最常見和最普遍的.
我們可以利用CallerMemberNameAttribute特性來簡(jiǎn)化一下,這個(gè)特性可以根據(jù)調(diào)用方來決定傳入哪個(gè)屬性的名字.:
- protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
- {
- var eventHandler = this.PropertyChanged;
- if (eventHandler != null)
- eventHandler(this, new PropertyChangedEventArgs(propertyName));
- }
這樣我們?cè)谡{(diào)用時(shí)可以這樣調(diào)用:
NotifyPropertyChange("ID") 改為:OnPropertyChanged();
INotifyPropertyChanged接口的***實(shí)現(xiàn)方式:
這個(gè)所謂的***實(shí)現(xiàn)方式 是channel 9的視頻中說的,實(shí)現(xiàn)方式如下:
- 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(this, new PropertyChangedEventArgs(propertyName));
- }
- }
相應(yīng)的調(diào)用方式進(jìn)一步簡(jiǎn)化:
- private string name;
- public string Name
- {
- get { return name; }
- set
- { this.SetProperty(ref this.name, value); }
- }
本文鏈接:http://www.cnblogs.com/bcmeng/p/3966931.html