如何在另一个类中触发一个事件



在我的代码中的PluginManager事件PluginEvent被触发后已添加插件。但是我想让事件也在测试类中触发。不知怎么的,我解决不了这个问题。该事件仅在PluginManager类中触发。我读了一些如何创建事件之类的文章,但我更困惑了


<

PluginManager类/strong>

public class PluginEventArgs
{
    public PluginEventArgs(string s) { Text = s; }
    public String Text { get; private set; } // readonly
}
public class PluginManager
{
    // Declare the delegate (if using non-generic pattern).
    public delegate void PluginEventHandler(object sender, PluginEventArgs e);
    // Declare the event.
    public event PluginEventHandler PluginEvent;
    protected virtual void RaiseSampleEvent(string message)
    {
        if (PluginEvent != null)
        PluginEvent(this, new PluginEventArgs(message));
    }

    public PluginManager()
    {
        PluginEvent += PluginManager_PluginEvent;
        SomeMethod();
    }
    void PluginManager_PluginEvent(object sender, PluginEventArgs e)
    {
       //This event gets triggered =)
    }
    public void SomeMethod()
    {
        //Code
        RaiseSampleEvent("Name of the Plugin");
        //Code
    }

 }

测试类:

class test
{
    public test()
    {
        PluginManager pluginMg = new PluginManager();
        pluginMg.PluginEvent += pluginMg_PluginEvent;
    }
    //I want this event to get triggered when a new plugin has been found
    void pluginMg_PluginEvent(object sender, PluginEventArgs e)
    {
        MessageBox.Show(e.Text);
    }
}

我怎样才能在测试类中触发事件?谢谢你的建议!

除了一个逻辑错误,你实际上做得很对。

在你的测试类中,你使用构造函数创建PluginManager。PluginManager的构造函数首先订阅事件,然后引发事件。

之后你将订阅那个事件。

简单的问题是,当您引发事件时,您的测试类尚未订阅。当您再次触发该事件时,一切都会正常运行。

另一件事是,我将使用通用的EventHandler类,而不是创建自己的委托。这使你的代码更干净,每个人都知道这意味着一个事件的第一眼。

从EventArgs中继承PlugInEventArgs,然后使用eventandler。

在你的PluginManager类中,你不应该订阅你自己的事件PluginEvent,你应该订阅一个外部事件或者只是引发PluginEvent。让我给你一个例子:

public class PluginEventArgs
{
    public PluginEventArgs(string s) { Text = s; }
    public String Text { get; private set; } // readonly
}
public class OtherClass
{
    public event PluginEventHandler PluginEvent;
    private void RaiseEvent()
    {
        if (null != PluginEvent)
            PluginEvent(this, new PluginEventArgs("some message"));
    }
}
public delegate void PluginEventHandler(object sender, PluginEventArgs e);
public class PluginManager
{
    public event PluginEventHandler PluginEvent;
    private OtherClass otherClass;
    protected virtual void RaiseSampleEvent(string message)
    {
        if (PluginEvent != null)
            PluginEvent(this, new PluginEventArgs(message));
    }
    public PluginManager(OtherClass otherClass)
    {
        this.otherClass = otherClass;
        this.otherClass.PluginEvent += otherClass_PluginEvent;
        SomeMethod();
    }
    void otherClass_PluginEvent(object sender, PluginEventArgs e)
    {
        if (PluginEvent != null)
            PluginEvent(sender, e); // this way the original sender and args are transferred.
    }
    public void SomeMethod()
    {
        //Code
        RaiseSampleEvent("Name of the Plugin");
        //Code
    }

}
class test
{
    public test()
    {
        OtherClass otherClass = new OtherClass();
        PluginManager pluginMg = new PluginManager(otherClass);
        pluginMg.PluginEvent += pluginMg_PluginEvent;
    }
    //I want this event to get triggered when a new plugin has been found
    void pluginMg_PluginEvent(object sender, PluginEventArgs e)
    {
        MessageBox.Show(e.Text);
    }
}

相关内容

  • 没有找到相关文章

最新更新