How to implement INotifyPropertyChanged with the nameof, and not with magic strings?

I read about the new nameof key in C # 6. I want to know how I can implement INotifyPropertyChanged with this keyword, what are the prerequisites (of course, except for C # 6) and how will this affect the performance of my MVVM application?

+7
source share
5 answers

It will look like this:

 public string Foo { get { return this.foo; } set { if (value != this.foo) { this.foo = value; OnPropertyChanged(nameof(Foo)); } } } 

At compile time, nameof(Foo) will be replaced with the string "Foo", so it should be very efficient. This is not a reflection.

+10
source

It is just a matter of using nameof() instead of the magic string. The following is an example of my related blog article:

 private string currentTime; public string CurrentTime { get { return this.currentTime; } set { this.currentTime = value; this.OnPropertyChanged(nameof(CurrentTime)); } } 

Since it is evaluated at compile time , it is more efficient than any of the existing alternatives (which are also mentioned in the blog post).

+6
source

Here is a complete sample class code using the new C # 6.0 sugar:

 public class ServerViewModel : INotifyPropertyChanged { private string _server; public string Server { get { return _server; } set { _server = value; OnPropertyChanged(nameof(Server)); } } private int _port; public int Port { get { return _port; } set { _port = value; OnPropertyChanged(nameof(Port)); } } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } 

In doing so, you get the operator nameof() , the operator with the null condition ?. and a function with an expression (definition of OnPropertyChanged ).

+3
source

I found it much easier to use PropertyChanged.Fody, because in the end you have fewer errors and a hell of a lot of clean code, see https://github.com/Fody/PropertyChanged

All you have to do is set the ImplementPropertyChanged attribute to the class:

 [ImplementPropertyChanged] public class Person { public string GivenNames { get; set; } public string FamilyName { get; set; } public string FullName { get { return string.Format("{0} {1}", GivenNames, FamilyName); } } } 

And after assembly, it is converted to:

 public class Person : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; string givenNames; public string GivenNames { get { return givenNames; } set { if (value != givenNames) { givenNames = value; OnPropertyChanged("GivenNames"); OnPropertyChanged("FullName"); } } } string familyName; public string FamilyName { get { return familyName; } set { if (value != familyName) { familyName = value; OnPropertyChanged("FamilyName"); OnPropertyChanged("FullName"); } } } public string FullName { get { return string.Format("{0} {1}", GivenNames, FamilyName); } } public virtual void OnPropertyChanged(string propertyName) { var propertyChanged = PropertyChanged; if (propertyChanged != null) { propertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } 
+3
source

See the documentation for the INotifyPropertyChanged.PropertyChanged event .

 private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } 
0
source

Source: https://habr.com/ru/post/979498/


All Articles