我想要一個州級班級,然後將您的活動附加到使用INotifyPropertyChanged
。
public class State: INotifyPropertyChanged
{
private string a;
private string b;
private string c;
private string d;
public event PropertyChangedEventHandler PropertyChanged;
// This method is called by the Set accessor of each property.
// The CallerMemberName attribute that is applied to the optional propertyName
// parameter causes the property name of the caller to be substituted as an argument.
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public string A
{
get { return this.a; }
set
{
if (value != this.a)
{
this.a= value;
NotifyPropertyChanged();
}
}
}
... so on and so forth ...
}
現在,所有你需要做的就是分配處理程序的屬性更改......
var myState = new State();
myState += (sender,args) =>
{
if(myState.A == myState.B && myState.C != myState.D)
{
// do stuff
}
};
我使用的原因界面使對象可以在ObservableCollection
中使用太多,如果需要。這樣你可以同時管理多個狀態。
您可以檢查'Update'並在由'a','b','c'和'd'的setters調用的函數中激發事件。 – Blorgbeard
我也考慮過這個問題,但有一個問題是如果更新條件發生變化,我需要創建更多自定義設置器或從現有屬性中刪除自定義設置器。基本上,我期待以最乾淨的方式做到這一點。 –