当变量发生变化时是否会引发事件?
Is there an event that will raise when a variable has changed?
我知道正确的做法是创建一个 class,在 said class 中创建一个事件,然后在程序的另一部分使用 said class变量将被更改(例如,在计算器的等号按钮中使用 said class,以便事件处理程序知道变量已更改,因为将触发事件)。但是,为了简化我的代码,我正在寻找一种方法来直接监视变量而无需无限 loop/timer 并在它发生变化时引发事件。有这样的事吗?如果没有,除了我提到的那个之外还有其他选择吗?
以下是我要提及的内容:
更改变量的代码 -> 另一段代码(不是循环)监视更改,如果有更改则抛出事件 -> 事件处理程序
你不能用字段来做,但是用属性来做:
class SomeClass : INotifyPropertyChanged
{
private string someProperty;
public string SomeProperty
{
get { return someProperty; }
set { someProperty = value; OnPropertyChanged(); }
}
private void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged = delegate {};
}
编辑 (.net 4.0)
class SomeClass : INotifyPropertyChanged
{
private string someProperty;
public string SomeProperty
{
get { return someProperty; }
set { someProperty = value; OnPropertyChanged("SomeProperty"); }
}
private void OnPropertyChanged(string propertyName)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged = delegate {};
}
编辑(Winforms 示例)
public partial class Form1 : Form
{
private SomeClass theObject = new SomeClass(); //keep a reference of the object.
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//here we do the binding... we want the 'Text' Property of the control to change if the 'SomeProperty' changes OnPropertyChanged
textBox1.DataBindings.Add("Text",theObject,"SomeProperty",false,DataSourceUpdateMode.OnPropertyChanged);
}
private void button1_Click(object sender, EventArgs e)
{
theObject.SomeProperty = "This works!"; //just a test button that changes the property...
}
}
虽然我知道在 Stack Overflow 上这通常是不受欢迎的做法,但您可能会发现我的项目 NMF Expressions 很有趣:http://nmfexpressions.codeplex.com/
基本上,该项目旨在让您编写如下内容:
var myObservedVariable = Observable.Expression(() => whatever you want)
在这种情况下,myObservedVariable
将属于 INotifyValue<T>
,它提供 ValueChanged
事件。或者,您可以使用查询语法。或者,您可以查看其他类似的框架,例如 Obtics, BindableLINQ or ContinuousLINQ. A comparison of the latter was done in Bindable Linq vs. Continuous Linq.
但是,这仅在非常强大的假设下有效,例如您正在使用的所有对象模型都完全支持 INotifyPropertyChanged
和 INotifyCollectionChanged
。
除了@Florian 的回答,您还可以在编译时使用 Fody.PropertyChanged.
注入 INotifyPropertyChanged 接口的实现
我知道正确的做法是创建一个 class,在 said class 中创建一个事件,然后在程序的另一部分使用 said class变量将被更改(例如,在计算器的等号按钮中使用 said class,以便事件处理程序知道变量已更改,因为将触发事件)。但是,为了简化我的代码,我正在寻找一种方法来直接监视变量而无需无限 loop/timer 并在它发生变化时引发事件。有这样的事吗?如果没有,除了我提到的那个之外还有其他选择吗?
以下是我要提及的内容:
更改变量的代码 -> 另一段代码(不是循环)监视更改,如果有更改则抛出事件 -> 事件处理程序
你不能用字段来做,但是用属性来做:
class SomeClass : INotifyPropertyChanged
{
private string someProperty;
public string SomeProperty
{
get { return someProperty; }
set { someProperty = value; OnPropertyChanged(); }
}
private void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged = delegate {};
}
编辑 (.net 4.0)
class SomeClass : INotifyPropertyChanged
{
private string someProperty;
public string SomeProperty
{
get { return someProperty; }
set { someProperty = value; OnPropertyChanged("SomeProperty"); }
}
private void OnPropertyChanged(string propertyName)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged = delegate {};
}
编辑(Winforms 示例)
public partial class Form1 : Form
{
private SomeClass theObject = new SomeClass(); //keep a reference of the object.
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//here we do the binding... we want the 'Text' Property of the control to change if the 'SomeProperty' changes OnPropertyChanged
textBox1.DataBindings.Add("Text",theObject,"SomeProperty",false,DataSourceUpdateMode.OnPropertyChanged);
}
private void button1_Click(object sender, EventArgs e)
{
theObject.SomeProperty = "This works!"; //just a test button that changes the property...
}
}
虽然我知道在 Stack Overflow 上这通常是不受欢迎的做法,但您可能会发现我的项目 NMF Expressions 很有趣:http://nmfexpressions.codeplex.com/
基本上,该项目旨在让您编写如下内容:
var myObservedVariable = Observable.Expression(() => whatever you want)
在这种情况下,myObservedVariable
将属于 INotifyValue<T>
,它提供 ValueChanged
事件。或者,您可以使用查询语法。或者,您可以查看其他类似的框架,例如 Obtics, BindableLINQ or ContinuousLINQ. A comparison of the latter was done in Bindable Linq vs. Continuous Linq.
但是,这仅在非常强大的假设下有效,例如您正在使用的所有对象模型都完全支持 INotifyPropertyChanged
和 INotifyCollectionChanged
。
除了@Florian 的回答,您还可以在编译时使用 Fody.PropertyChanged.
注入 INotifyPropertyChanged 接口的实现