我有一个带有TimeHandler for TimeChanged的类。创建了这个类的几个实例。在课程的一个实例中,时间可以改变。我希望所有实例都能对变化的时间做出反应。
示例:
public class MainClass
{
public MainClass()
{
}
public event EventHandler TimeChanged;
private virtual void OnTimeChanged()
{
EventHandler handler = TimeChanged;
if(handler != null)
{
handler(this, eventArgs.Empty);
}
}
}
public class AnotherClass
{
public AnotherClass()
{
MainClass _mainClass = new MainClass();
}
private void OnLoaded()
{
_mainClass.TimeChanged += HandleTimeChanged;
}
private void HandleTimeChanged
{
//Some stuff happens with new time, etc.
}
}
public class TimeClass
{
MainClass _class = new MainClass();
TimeSpan _timeValue;
private void ChangeTime()
{
// This is where the _timeValue is changed
// All instances of MainClass, subscribed to TimeChanged event to be notified with the new value selected.
}
}
所以,基本上当我在包含MainClass实例的TimeClass中更改时间时,我想触发TimeChanged事件,MainClass的所有实例(如在AnotherClass中)应该响应他们订阅的TimeChanged事件。 / p>
希望这可以解释这种情况。欢迎任何建议。感谢。