Qualcuno sa se BindableBase è ancora valido o dovremmo attenerci agli eventi di INotifyChanged? Sembra che BindableBase abbia perso rapidamente la sua lucentezza. Grazie per qualsiasi informazione che potete fornire.BindableBase vs INotifyChanged
risposta
INotifyPropertyChanged
Il ViewModel dovrebbe implementare l'interfaccia INotifyPropertyChanged e risusciti ogniqualvolta le propertychanges
public class MyViewModel : INotifyPropertyChanged
{
private string _firstName;
public event PropertyChangedEventHandler PropertyChanged;
public string FirstName
{
get { return _firstName; }
set
{
if (_firstName == value)
return;
_firstName = value;
PropertyChanged(this, new PropertyChangedEventArgs("FirstName"));
}
}
}
}
problema è con ICommand interfaccia come la maggior parte del codice sono duplicati anche poiché passa stringa diventa soggetto a errori.
Mentre Bindablebase è una classe astratta che implementa l'interfaccia INotifyPropertyChanged e fornire SetProperty<T>
.È possibile ridurre il metodo set a una sola linea anche il parametro ref permette di aggiornare il suo valore. Il codice BindableBase qui sotto proviene da INotifyPropertyChanged, The .NET 4.5 Way - Revisited
public class MyViewModel : BindableBase
{
private string _firstName;
private string _lastName;
public string FirstName
{
get { return _firstName; }
set { SetProperty(ref _firstName, value); }
}
}
//Inside Bindable Base
public abstract class BindableBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
{
if (Equals(storage, value))
{
return false;
}
storage = value;
this.OnPropertyChanged(propertyName);
return true;
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler eventHandler = this.PropertyChanged;
if (eventHandler != null)
{
eventHandler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Non è una scelta tra questi due.
BindableBase implementa INotifyPropertyChanged.
Quindi se si utilizza BindableBase verrà utilizzato INotifyPropertyChanged.
InotifyPropertyChanged è più o meno obbligatorio quando si implementa MVVM utilizzando DataBinding.
Se utilizzare BindableBase o un'altra implementazione dipende dalla preferenza e dall'uso di Prism.
Quindi BindableBase è esclusivamente per Prism? Se volessi usare Prism e MVVM standard, dovrei limitarmi all'approccio INotifyPropertyChanged. – ChiliYago
@ChiliYago qualsiasi risposta su questo è opinione pura; sta a te decidere. Non esiste MVVM standard, è solo un insieme di linee guida che ti aiutano ad assegnare le responsabilità alle classi. Quale implementazione usi non è nel modello. –
Ad ampliare la risposta di Rohit, se si utilizza .NET 4.6 è possibile usufruire del gestore Null-condizionale e semplificare il metodo OnPropertyChanged nel seguente modo:
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
INotifyPropertyChanged, The .NET 4.6 Way lo spiega in modo più dettagliato.
Per gli antipasti, BindableBase è una cosa Prism, quindi non è rilevante per WPF vaniglia (anche se il concetto potrebbe essere). Potresti essere più specifico in ciò che stai chiedendo? – BradleyDotNET
Uso 'BindableBase' nei miei progetti Prism. È ancora abbastanza brillante :) –