Detailed description of inotifypropertychanged Interface

Source: Internet
Author: User
Tags blank page

In Windows Phone development 8.1: data binding, we learned the basics of data binding. in the next few articles, we will continue to learn more about data binding. today we will look at the implementation of the inotifypropertychanged interface, which is very important in data binding.

When to implement the inotifypropertychanged Interface

Official explanation: the inotifypropertychanged interface is used to send a notification that a property value has been changed to the client (usually the client that executes the binding. The official explanation is vague. It is estimated that I don't know when to implement the inotifypropertychanged interface after reading it. Xiaomeng makes a clear conclusion through actual tests:

First:OnetimeMode: it makes no sense, because its binding is only bound once at the beginning, and cannot be changed at all! Naturally, inotifypropertychanged is not implemented.

Then the oneway mode is used:We know that the oneway Mode means that every change to the binding source will notify the binding target, but the change to the binding target will not change the binding source. when the data entity class of the bound source does not implement the inotifypropertychanged interface, when we change the data source, we will find that the corresponding data on the UI of the bound target does not change immediately. so at this time, we need to implement the inotifypropertychanged interface.

The last is the twoway mode:In twoway mode, when the data entity class bound to the source does not implement the inotifypropertychanged interface, we find that. changes to the control will immediately change the data source, but changes to the data source, the bound target control will not change immediately! Therefore, when we need to change the corresponding UI immediately, we need to implement the inotifypropertychanged interface.

In short:The inotifypropertychanged interface must be implemented when the data source changes and the UI changes immediately.

We can use this example to clearly understand this point:

<Stackpanel> <textbox header = "no." text = "{binding ID, mode = onetime} "name =" tbxid "> </textbox> <textbox header =" title "text =" {binding title, mode = oneway} "name =" tbxtitle "> </textbox> <textbox header =" price "text =" {binding price, mode = twoway} "name =" tbxprice "> </textbox> <button content =" Modify the control value through the data source "Click =" button_click "> </button> <button content = "directly modify the control value" Click = "button_click_1"/> <button content = "Modify the data source value through the control" Click = "button_click_2"/> </stackpanel>

 

Background code:

Namespace inotifypropertychangeddemo {// <summary> /// you can use it to navigate to a blank page in the frame. /// </Summary> Public sealed partial class mainpage: Page {book = New Book (); Public mainpage () {This. initializecomponent (); this. navigationcachemode = navigationcachemode. required; book. id = 0; book. title = "ASP. net Development Manual "; book. price = 40; ST. datacontext = book;} private void button_click (Object sender, routedeventargs e) // modify the value of the control by modifying the data source {book. id = 100; book. price = 50; book. title = "SL open Publish manual ";} private async void button_click_1 (Object sender, routedeventargs e) // display the data source value {await new messagedialog (book. id. tostring () + "" + book. title. tostring () + "" + book. price. tostring ()). showasync ();} public class Book: inotifypropertychanged // The inotifypropertchanged interface defines an event to be executed when the property value changes. The event name is propertychanged. // This is the event {private int _ id; Public int ID {get {return _ id;} set {_ id = value; // yypropertychange ("ID") ;}} private string _ title; Public String title {get {return _ title ;}set {_ Title = value; // yypropertychange ("title") ;}} private double _ price; Public double price {get {return _ price ;}set {_ price = value; // yypropertychange ("price");} public event PR Opertychangedeventhandler propertychanged; // propertychangedeventargs type. This class is used to pass the name of the property that changes the value, and send a change notification to the property that has been changed on the client. The attribute name is of the string type. Private void policypropertychange (string propertyname) {If (propertychanged! = NULL) {// implement the propertychanged event based on the delegate class of the propertychanged event: propertychanged (this, new propertychangedeventargs (propertyname ));}}}}}

 

You can run this example to clearly understand the role of the inotifypropertychanged interface.

How to Implement the inotifypropertychanged Interface

The inotifypropertychanged interface is the most common and common implementation method.

We can use the callermembernameattribute feature to simplify the process. This feature can be used by the caller to determine the name of the attribute to be passed in .:

protected void OnPropertyChanged([CallerMemberName] string propertyName = null)        {            var eventHandler = this.PropertyChanged;            if (eventHandler != null)                eventHandler(this, new PropertyChangedEventArgs(propertyName));        }

 

In this way, we can call the following method during the call:

 

Notifypropertychange ("ID") to onpropertychanged ();

The best implementation method of inotifypropertychanged interface:

The so-called best implementation method is described in the video of Channel 9. The implementation method is as follows:

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));        }    }


The calling method is further simplified:

   private string name;        public string Name        {            get { return name; }            set            { this.SetProperty(ref this.name, value); }        }

It's time to go to bed. Continue tomorrow!

Detailed description of inotifypropertychanged Interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.