1
我希望运行一些会在某些计数器变化时发生的事件,例如,每次c#计数器变化事件
int counter;
更改其值,事件发生。 我有这样的事情从MSDN:
public class CounterChange:INotifyPropertyChanged
{
private int counter;
// Declare the event
public event PropertyChangedEventHandler PropertyChanged;
public CounterChange()
{
}
public CounterChange(int value)
{
this.counter = value;
}
public int Counter
{
get { return counter; }
set
{
counter = value;
// Call OnPropertyChanged whenever the property is updated
OnPropertyChanged("Counter");
}
}
// Create the OnPropertyChanged method to raise the event
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if(handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
但不知道下一步是什么。如何从程序中提升增量,并将方法连接到这些事件。
的可能重复[如何分派在C#中的事件](http://stackoverflow.com/questions/2448487/how-在-c-sharp中调度事件) – 2012-02-21 09:37:27
您的问题中未包含的一些信息是应该处理此事件的代码的解释。例如,你是自己编写一个事件处理程序,还是将该属性绑定到某个UI?您在上面显示的代码示例特定于WPF/SL(以及其他通用框架)。 – 2012-02-21 09:39:44