C# 每当函数返回true而不进行更新循环时引发事件
现在,我的程序中没有更新循环,为了将来的灵活性,我希望避免使用更新循环,但我不确定没有更新循环是否可行 如果我有一个布尔值,比如:C# 每当函数返回true而不进行更新循环时引发事件,c#,events,delegates,C#,Events,Delegates,现在,我的程序中没有更新循环,为了将来的灵活性,我希望避免使用更新循环,但我不确定没有更新循环是否可行 如果我有一个布尔值,比如:bool Update=>a==b&&c!=d,是否可以在该情况下自动调用函数 或者,我知道我可以这样做: CheckUpdate() { if(Update) *do something here* } 但是我希望消息在值为true时发送,而不是在更新循环检测到它为true时发送。我将有一个状态类,然后您可以使用INotifyPrope
bool Update=>a==b&&c!=d
,是否可以在该情况下自动调用函数
或者,我知道我可以这样做:
CheckUpdate()
{
if(Update)
*do something here*
}
但是我希望消息在值为true时发送,而不是在更新循环检测到它为true时发送。我将有一个状态类,然后您可以使用
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
}
};
我使用该接口的原因是,如果需要,该对象也可以在
可观测集合中使用。通过这种方式,您可以同时管理多个状态。我将拥有一个状态类,然后您可以使用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
}
};
我使用该接口的原因是,如果需要,该对象也可以在可观测集合中使用。这样,您可以同时管理多个状态。请尝试以下方法:
public class Foo
{
public Foo()
{
_update = () => a == b && c != d;
}
private Func<bool> _update;
private string a;
private string b;
private string c;
private string d;
private string e;
private string f;
private string g;
private string h;
private void CheckUpdate()
{
if (_update())
{
/*do something here*/
}
}
public string A { get { return a; } set { a = value; CheckUpdate(); } }
public string B { get { return b; } set { b = value; CheckUpdate(); } }
public string C { get { return c; } set { c = value; CheckUpdate(); } }
public string D { get { return d; } set { d = value; CheckUpdate(); } }
public string E { get { return e; } set { e = value; CheckUpdate(); } }
public string F { get { return f; } set { f = value; CheckUpdate(); } }
public string G { get { return g; } set { g = value; CheckUpdate(); } }
public string H { get { return h; } set { h = value; CheckUpdate(); } }
}
公共类Foo
{
公共食物(
{
_更新=()=>a==b&&c!=d;
}
私有函数更新;
私人字符串a;
私有字符串b;
私有字符串c;
私有字符串d;
私有字符串e;
私有字符串f;
私有字符串g;
私有字符串h;
私有void CheckUpdate()
{
如果(_update())
{
/*在这里做点什么*/
}
}
公共字符串A{get{return A;}set{A=value;CheckUpdate();}
公共字符串B{get{return B;}set{B=value;CheckUpdate();}
公共字符串C{get{return C;}set{C=value;CheckUpdate();}
公共字符串D{get{return D;}set{D=value;CheckUpdate();}
公共字符串E{get{return E;}set{E=value;CheckUpdate();}
公共字符串F{get{return F;}set{F=value;CheckUpdate();}
公共字符串G{get{return G;}set{G=value;CheckUpdate();}
公共字符串H{get{return H;}set{H=value;CheckUpdate();}
}
无论有多少属性,每次更新时只需调用CheckUpdate()
。然后,您可以随时更改\u update
,无论是编译时还是运行时,以满足您的需求。尝试以下操作:
public class Foo
{
public Foo()
{
_update = () => a == b && c != d;
}
private Func<bool> _update;
private string a;
private string b;
private string c;
private string d;
private string e;
private string f;
private string g;
private string h;
private void CheckUpdate()
{
if (_update())
{
/*do something here*/
}
}
public string A { get { return a; } set { a = value; CheckUpdate(); } }
public string B { get { return b; } set { b = value; CheckUpdate(); } }
public string C { get { return c; } set { c = value; CheckUpdate(); } }
public string D { get { return d; } set { d = value; CheckUpdate(); } }
public string E { get { return e; } set { e = value; CheckUpdate(); } }
public string F { get { return f; } set { f = value; CheckUpdate(); } }
public string G { get { return g; } set { g = value; CheckUpdate(); } }
public string H { get { return h; } set { h = value; CheckUpdate(); } }
}
公共类Foo
{
公共食物(
{
_更新=()=>a==b&&c!=d;
}
私有函数更新;
私人字符串a;
私有字符串b;
私有字符串c;
私有字符串d;
私有字符串e;
私有字符串f;
私有字符串g;
私有字符串h;
私有void CheckUpdate()
{
如果(_update())
{
/*在这里做点什么*/
}
}
公共字符串A{get{return A;}set{A=value;CheckUpdate();}
公共字符串B{get{return B;}set{B=value;CheckUpdate();}
公共字符串C{get{return C;}set{C=value;CheckUpdate();}
公共字符串D{get{return D;}set{D=value;CheckUpdate();}
公共字符串E{get{return E;}set{E=value;CheckUpdate();}
公共字符串F{get{return F;}set{F=value;CheckUpdate();}
公共字符串G{get{return G;}set{G=value;CheckUpdate();}
公共字符串H{get{return H;}set{H=value;CheckUpdate();}
}
无论有多少属性,每次更新时只需调用CheckUpdate()
。然后,您可以随时更改\u update
,无论是编译时还是运行时,以满足您的需求。您可以选中update
,并在函数中触发事件,该函数由a
、b
、c
和d
的设置程序调用。我也考虑过这一点,但一个问题是,如果更新条件发生更改,我将需要创建更多自定义设置器或从现有属性中删除自定义设置器。基本上,我希望以最简洁的方式来实现这一点。您可以检查Update
并在函数中激发事件,该函数是从a
、b
、c
和d
的设置程序调用的。我也考虑过这一点,但有一个问题是如果更新条件发生变化,我需要创建更多自定义设置器或从现有属性中删除自定义设置器。基本上,我希望用最干净的方式来做这件事。