0

我正在检查我的代码并重做所有事件,以便它们符合本文的要求,但我遇到了障碍。这是我的代码:

脚本.cs

public EventHandler<ScriptEvent> Load;

protected virtual void OnLoad(string file)
{
    EventHandler<ScriptEvent> handler = Load;

    if(Load != null)
       Load(this, new ScriptEvent(file));
}

和脚本事件:

脚本事件.cs

public class ScriptEvent : EventArgs
{
    private string m_File;

    public string File
    {
        get { return m_File; }
    }        

    public ScriptEvent(string file)
    {
       this.m_File = file;
    }
}

问题是我无法弄清楚如何制作它,以便我的表单可以处理事件。例如,在 Script.cs 中加载了一个文件,然后当调用 Script.cs 中的 OnLoad(...) 时,表单会显示该文件。如果这令人困惑:

1) Script.cs 加载一个文件并触发 OnLoad(file)

2) Form1 接上 OnLoad 并做任何事情。

我认为这与我之前的做法类似(script.OnLoad += script_OnLoad(...)),但我很难过。

4

1 回答 1

1

I think you might want:

script.Load += script_OnLoad;

This will add your handler to the event. Then when the OnLoad method is called, it will invoke your handler.

Your script_OnLoad method needs to look like this:

void script_OnLoad(object sender, ScriptEvent args)
{
     // your code here - you can get the file name out of the args.File
}

Your Script.cs can be cut down to:

public EventHandler<ScriptEvent> Load;

protected virtual void OnLoad(string file)
{
    if(Load != null)
        Load(this, new ScriptEvent(file));
}
于 2013-12-13T04:50:48.850 回答