简体   繁体   English

将ClassA中的事件处理程序添加到ClassB中调用的事件中

[英]Add event handler from ClassA to an event called in ClassB

I am really stuck with how to use events in my app. 我真的很困惑如何在我的应用程序中使用事件。 I have two files: 我有两个文件:

RTimer.cs
SettingsForm.cs

I have a timer set up in RTimer.cs that initializes the timer and sets the interval and such. 我在RTimer.cs中设置了一个定时器,用于初始化定时器并设置间隔等。

I have a method in SettingsForm.cs that needs to do something every time the timer ticks. 我在SettingsForm.cs中有一个方法,每次定时器滴答时都需要做一些事情。 This file also has a method that sets the timers interval via a SetTimer() method in the RTimer.cs 该文件还有一个方法,通过RTimer.csSetTimer()方法设置定时器间隔

I cant for the life of me figure out how to get the Tick event to call the method from the other class, or get the method in the other class to subscribe to the Tick event. 我不能让我的生活弄清楚如何让Tick事件从另一个类调用该方法,或者获取另一个类中的方法来订阅Tick事件。

You can implement this way : 你可以这样实现:

class SettingsForm
{
    public void OnTimerEvent(object sender, EventArgs e)
    {
        throw new NotImplementedException();
    }
}

class RTimer
{
    Timer timer = new Timer();
    public void StartTimer(SettingsForm settingForm)
    {
        timer.Tick += settingForm.OnTimerEvent;
        timer.Interval = 5000;
        timer.Enabled = true;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM