In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use the INotifyPropertyChanged interface of C#". In the daily operation, I believe many people have doubts about how to use the INotifyPropertyChanged interface of C#. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use the INotifyPropertyChanged interface of C#". Next, please follow the editor to study!
INotifyPropertyChanged:
The interface contains an event that executes when a property changes.
/ / Summary: / / notifies the client that the property value has changed. Public interface INotifyPropertyChanged {/ Summary: / / occurs when the property value changes. Event PropertyChangedEventHandler PropertyChanged;}
Next, use a simple example to illustrate its simple use (demonstrated by most common practices):
1. Define a ViewModelBase inheritance INotifyPropertyChanged interface and add a virtual function to inherit the properties of the subclass for change notification
There are two attributes in 2.MainViewModel. Code,Name notifies the call when the Set is changed.
Public class ViewModelBase: INotifyPropertyChanged {public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged (string propertyName) {if (this.PropertyChanged! = null) this.PropertyChanged (this, new PropertyChangedEventArgs (propertyName));} public class MainViewModel: ViewModelBase {private string name; private string code; public string Name {get {return name } set {name = value; OnPropertyChanged ("Name");}} public string Code {get {return code;} set {code = value; OnPropertyChanged ("Code");}
As in the above code, you should notice that when each property calls OnPropertyChanged, you need to pass its own property name, isn't that superfluous? Yes, a lot.
Reform
See some articles to change the parameters of the base class to the expression tree, so when implementing, pass a Lambda expression, I think this is not a temporary cure? It is as follows:
Note: the original direct transfer of a fixed string type argument, not to mention the performance problem of lambda, the same problem you still need to write this parameter. This is not recommended!
CallerMemberName
This class inherits from Attribute. It is not difficult to see that this class belongs to a special effects class defined on methods and properties. Implementing this feature allows you to obtain the method or property name of the method caller.
/ / Summary: / / allows you to get the method or property name of the method caller. [AttributeUsage (AttributeTargets.Parameter, Inherited = false)] public sealed class CallerMemberNameAttribute: Attribute {/ Abstract: / / initialize a new instance of the System.Runtime.CompilerServices.CallerMemberNameAttribute class. Public CallerMemberNameAttribute ();}
Transform ViewModelBase:
After the transformation, did you find an obvious difference:
You don't have to pass parameters, you don't have to write lambda expressions, and you don't have to worry about the security of the parameters they pass, just read the attribute name!
At this point, the study on "how to use the INotifyPropertyChanged interface of C#" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.