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.
- 1. ID vs UniqueID vs ClientID vs UniqueClientID vs StaticClientID?
- 2. VS 2008 vs VS 2008 Express
- 3. .NET vs ASP.NET vs CLR vs ASP
- 4. Atomikos vs JOTM vs Bitronix vs?
- 5. Accumulare vs piega vs ridurre vs comprimere
- 6. ACE vs Boost vs Poco vs wxWidgets
- 7. VS 2013 MSTest vs nUnit vs xUnit
- 8. control.BeginInvoke() Vs Dispatcher Vs SynchronizationContext Vs .. - AFFIDABILITÀ
- 9. Exec vs ExecWait vs ExecShell vs nsExec :: Exec vs nsExec :: ExecToLog vs nsExec :: ExecToStack vs ExecDos vs ExeCmd
- 10. bigtable vs cassandra vs simpledb vs dynamo vs couchdb vs hypertable vs riak vs hbase, cosa hanno in comune?
- 11. e vs * e | vs +
- 12. SpiderMonkey vs JavaScriptCore vs?
- 13. * vs ** vs *** in Proguard?
- 14. bundler vs RVM vs gemme vs RubyGems vs gemsets vs system ruby
- 15. Misurare il tempo in Linux - tempo vs orologio vs getrusage vs clock_gettime vs gettimeofday vs timespec_get?
- 16. innerText vs innerHtml vs label vs text vs textContent vs outerText
- 17. Mathematica: non valutata vs Defer vs attesa vs HoldForm vs HoldAllComplete vs etc etc
- 18. Iterator vs. Reference vs. pointer
- 19. Selenium vs. SimpleTest vs. WatiN
- 20. BasicHttpBinding vs WsHttpBinding vs webHttpBinding
- 21. Boo vs C# vs Python?
- 22. StringBuffer vs StringBuilder Vs StringTokenizer
- 23. JSF vs Facelets vs JSP
- 24. Qcodo vs. CakePHP vs. Zend
- 25. select vs poll vs epoll
- 26. RazorEngine vs RazorTemplates vs RazorMachine
- 27. cudaStreamSynchronize vs CudaDeviceSynchronize vs cudaThreadSynchronize
- 28. Bitmap vs ImageView vs Drawable
- 29. gruntjs vs yeoman vs guard
- 30. migrator.net vs fluentmigrator vs migsharp
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 :) –