2012-01-31 3 views
6

Ho questo BaseClass:Ereditare da una baseclass che implementa INotifyPropertyChanged?

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

    public event PropertyChangedEventHandler PropertyChanged; 
} 

e un altra classe:

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 è qui (2times), ha sottolineato rosso, si dice:

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') 

Quello che sto fare male? Ho spazzato solo il PropertyChangedEvent ad una nuova classe: BaseViewModel ..

+0

Link spostato di nuovo: http://george.softumus.com/inotifypropertychanged-and-magic-strings/ –

risposta

6

È Non può generare l'evento al di fuori della classe è dichiarata, utilizzare il metodo nella classe base per sollevarlo (fanno OnPropertyChangedprotected).

+0

non ancora funzionante, modificato solo – eMi

+1

@eMi : Ho detto che dovresti usare il metodo, renderlo 'protetto' è solo il passo necessario per farlo. Non cercare di aumentare l'evento. –

3

Cambio classe derivata come segue:

public class SchemaDifferenceViewModel : BaseViewModel 
{ 
    private string firstSchemaToCompare; 

    public string FirstSchemaToCompare 
    { 
     get { return firstSchemaToCompare; } 
     set 
     { 
      firstSchemaToCompare = value; 
      OnPropertyChanged("FirstSchemaToCompare"); 
     } 
    } 
0

Fare una classe base per INPC è cattivo design a mio parere.

Questo è il luogo in cui libro di testo è possibile utilizzare un mixin

In breve, consente di fornire un'implementazione di default di membri di un'interfaccia. Sarai comunque in grado di ereditare da una classe realmente interessante =)