1

我只是在学习事件、代表和订阅者。在过去的两天里,我一直在研究和思考这一切。我无法访问在 EventArgs e 值中传递的信息。我有一个想要打开的已保存项目。必要形式的状态被反序列化成字典。命中一个循环,引发 UnpackRequest 传递键/值。

ProjectManager.cs 文件:

public delegate void EventHandler<TArgs>(object sender, TArgs args) where TArgs : EventArgs;
public event EventHandler<UnpackEventArgs> UnpackRequest;

然后再往下:

ProjectManager.cs 文件:

//Raise a UnpackEvent //took out virtual
protected  void RaiseUnpackRequest(string key, object value)
{
    if (UnpackRequest != null) //something has been added to the list?
    {
        UnpackEventArgs e = new UnpackEventArgs(key, value);
        UnpackRequest(this, e);
    }
}

然后在 open 方法中,在字典填充了每种形式的状态之后:

ProjectManager.cs 文件:

foreach (var pair in dictPackState) {
    string _key = pair.Key;
    dictUnpackedState[_key] = dictPackState[_key];
    RaiseUnpackRequest(pair.Key, pair.Value); //raises the event.
    }

我有一个活动课程:

public class UnpackEventArgs : EventArgs
{
    private string strKey;
    private object objValue;

    public UnpackEventArgs(string key, object value)
    {
        this.strKey = key;
        this.objValue = value;
    }
    //Public property to read the key/value ..and get them out
    public string Key
    {
        get { return strKey; }  
    }
    public object Value
    { 
        get { return objValue; }
    }
}

我仍在研究添加订阅者的代码以及项目组件如何在各个表单中重新构成。但是我现在尝试处理的部分是在 MainForm.cs 中,我使用通过的争论来处理 Unpacked Request。我的 e 包含键值,键表示将值发送到哪里(即表单对象)。

private void HandleUnpackRequest(object sender, EventArgs e)
{
    //reflection happens here. turn key into object
    //why can't i get e.key ??? 
    object holder = e; //holder = VBTools.UnpackEventArgs key... value...all info

    //object goToUnpack = (e.key).GetType();
    //goToUnpack.unpackState(e.value);
}

我想我包括了所有必要的部分以获得任何帮助?!谢谢!

4

1 回答 1

6

改变这个:

private void HandleUnpackRequest(object sender, EventArgs e) 

对此:

private void HandleUnpackRequest(object sender, UnpackEventArgs e) 

记住您的事件处理程序声明:

public event EventHandler<UnpackEventArgs> UnpackRequest;
于 2012-04-13T20:28:54.620 回答