Có ai biết nếu BindableBase vẫn là khả thi hay chúng ta nên gắn bó với sự kiện INotifyChanged? Có vẻ như BindableBase đã nhanh chóng mất độ bóng của nó. Cảm ơn bạn cho bất kỳ thông tin mà bạn có thể cung cấp.BindableBase vs INotifyChanged
Trả lời
INotifyPropertyChanged
ViewModel nên thực hiện các giao diện INotifyPropertyChanged và nên nâng nó bất cứ khi nào 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"));
}
}
}
}
Vấn đề là với giao diện ICommand như hầu hết các mã được nhân đôi cũng vì nó đi chuỗi nó trở thành dễ bị lỗi.
Trong khi Bindablebase là một lớp trừu tượng mà thực hiện giao diện INotifyPropertyChanged và cung cấp SetProperty<T>
.Bạn có thể làm giảm phương pháp thiết lập để chỉ một dòng cũng tham số ref cho phép bạn cập nhật giá trị của nó. Mã BindableBase dưới đây xuất phát từ 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));
}
}
}
Nó không phải là một sự lựa chọn giữa hai.
BindableBase thực hiện INotifyPropertyChanged.
Vì vậy, nếu bạn sử dụng BindableBase, bạn sẽ sử dụng INotifyPropertyChanged.
INotifyPropertyChanged ít nhiều bị bắt buộc khi triển khai MVVM bằng cách sử dụng DataBinding.
Việc sử dụng BindableBase hay triển khai khác tùy thuộc vào sở thích và sử dụng Prism.
Vì vậy, BindableBase là dành riêng cho Prism? Nếu tôi muốn sử dụng Prism và MVVM chuẩn, tôi chỉ nên gắn bó với cách tiếp cận INotifyPropertyChanged. – ChiliYago
@ChiliYago bất kỳ câu trả lời nào là ý kiến thuần túy; tuỳ bạn quyết định. Không có MVVM chuẩn, nó chỉ là một bộ các nguyên tắc giúp bạn phân công trách nhiệm cho các lớp học. Triển khai nào bạn sử dụng không có trong mẫu. –
Để mở rộng về câu trả lời Rohit, nếu bạn đang sử dụng .NET 4.6 bạn có thể tận dụng lợi thế của các nhà điều hành Null-có điều kiện và đơn giản hóa các phương pháp OnPropertyChanged theo cách sau:
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
INotifyPropertyChanged, The .NET 4.6 Way giải thích chi tiết hơn.
- 1. J2ME VS Android VS iPhone VS Symbian VS Windows CE
- 2. Exec vs ExecWait vs ExecShell vs nsExec :: Exec vs nsExec :: ExecToLog vs nsExec :: ExecToStack vs ExecDos vs ExeCmd
- 3. bigtable vs cassandra vs simpledb vs dynamo vs couchdb vs hypertable vs riak vs hbase, họ có điểm gì chung?
- 4. ACE vs Boost vs Poco vs wxWidgets
- 5. NetSqlAzMan vs AzMan vs (?????)
- 6. & vs * và | vs +
- 7. * vs ** vs *** trong Proguard?
- 8. Mathematica: Unevaluated vs Defer vs Hold vs HoldForm vs HoldAllComplete vs etc etc
- 9. ember.js + tay lái: render vs outlet vs vs vs vs control
- 10. innerText vs innerHtml vs nhãn vs văn bản vs textContent vs outerText
- 11. Htmlentities vs addslashes vs mysqli_real_escape_string
- 12. Delegate.CreateDelegate vs DynamicMethod vs Biểu
- 13. RazorEngine vs RazorTemplates vs RazorMachine
- 14. HttpRequest vs HttpRequestMessage vs HttpRequestBase
- 15. getchar_unlocked() VS scanf() VS cin
- 16. gruntjs vs yeoman vs guard
- 17. pycuda vs theano vs pylearn2
- 18. Coroutine vs Continuation vs Generator
- 19. DataView.RowFilter Vs DataTable.Select() vs DataTable.Rows.Find()
- 20. Shmem vs tmpfs vs mmap
- 21. Processing.js vs. CAKE vs. Raphael.js
- 22. Mongoose.model vs Connection.model vs Model.model
- 23. MySQL vs Firebird vs SQLite
- 24. window.onload vs. body.onload vs. document.onready
- 25. Selenium vs. SimpleTest vs. WatiN
- 26. Matlab vs Aforge vs OpenCV
- 27. select vs poll vs epoll
- 28. localStorage vs sessionStorage vs cookies
- 29. fadeIn vs fadeOut vs fadeTo
- 30. int vs NSNumber vs NSInteger
Đối với người mới bắt đầu, BindableBase là một điều lăng kính, vì vậy không liên quan đến vanilla WPF (mặc dù khái niệm có thể là). Bạn có thể cụ thể hơn trong những gì bạn đang yêu cầu? – BradleyDotNET
Tôi sử dụng 'BindableBase' trong các dự án Prism của tôi. Nó vẫn khá bóng bẩy :) –