Inherited from a single base layer that implements INotifyPropertyChanged?

I have this BaseClass:

public class BaseViewModel : INotifyPropertyChanged { protected void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } public event PropertyChangedEventHandler PropertyChanged; } 

and another class:

 public class SchemaDifferenceViewModel : BaseViewModel { private string firstSchemaToCompare; public string FirstSchemaToCompare { get { return firstSchemaToCompare; } set { firstSchemaToCompare = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("FirstSchemaToCompare")); //StartCommand.RaiseCanExecuteChanged(); } } } 

PropertyChanged here (2 times), red is underlined, it says:

 Error 1 The event BaseViewModel.PropertyChanged' can only appear on the left hand side of += or -= (except when used from within the type 'SchemaDifferenceFinder.ViewModel.BaseViewModel') 

What am I doing wrong? I just moved PropertyChangedEvent to a new class: BaseViewModel ..

+6
source share
3 answers

You cannot raise an event outside the class in which it is declared, use the method in the base class to raise it (make OnPropertyChanged protected ).

+6
source

Modify the derived class as follows:

 public class SchemaDifferenceViewModel : BaseViewModel { private string firstSchemaToCompare; public string FirstSchemaToCompare { get { return firstSchemaToCompare; } set { firstSchemaToCompare = value; OnPropertyChanged("FirstSchemaToCompare"); } } 
+3
source

Creating a base class for INPC is a bad design, in my opinion.

This is the place in the tutorial where you can use mixin

In short, it allows you to provide a default implementation of interface elements. You can still inherit from a really interesting class =)

0
source

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


All Articles