Bạn sẽ cần phải phá vỡ ở trên ra cho các thành viên private/public, do đó bạn không nhận được vấn đề đệ quy:
private float _inverseMass;
public float inverseMass
{
get { return this._inverseMass; }
set { this._inverseMass = value; onMassChanged(); }
}
Tuy nhiên, có bạn nhìn vào giao diện INotifyPropertyChanged
? Nó làm khá nhiều những gì bạn đang tìm kiếm, và tùy thuộc vào những gì bạn đang viết nó có thể được hỗ trợ nguyên bản.
public class MyClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String property)
{
var event = this.PropertyChanged;
if (event != null)
{
event(this, new PropertyChangedEventArgs(property));
}
}
private float _inverseMass;
public float inverseMass
{
get { return this._inverseMass; }
set { this._inverseMass = value; NotifyPropertyChanged("inverseMass"); }
}
}
Nguồn
2012-01-03 20:09:23
Bạn không thể làm điều đó trong C#. Bạn phải sử dụng một trường sao lưu. –
Câu trả lời là CÓ .... nó sẽ tạo ra một vòng lặp liên tục. –
@UweKeim, đó phải là câu trả lời. – driis