我想要一个州级班级,然后将您的活动附加到使用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
我也考虑过这个问题,但有一个问题是如果更新条件发生变化,我需要创建更多自定义设置器或从现有属性中删除自定义设置器。基本上,我期待以最干净的方式做到这一点。 –