无法访问要在 HandleRequest 中使用的 EventArgs e 值

本文关键字:EventArgs 访问 HandleRequest | 更新日期: 2023-09-27 18:31:08

我只是在学习事件、代表和订阅者。在过去的两天里,我花了很长时间研究并围绕这一切进行思考。我无法访问在我的 EventArgs e 值中传递的信息。我有一个保存的项目想要打开。必要表单的状态被反序列化为字典。命中一个循环,该循环引发 UnpackRequest 传递键/值。

项目经理.cs文件:

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

再往下走

项目经理.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 方法中,在字典填充了每种形式的状态之后:

项目经理.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在那里我使用传递的参数处理解压缩请求。我的 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);
}

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

无法访问要在 HandleRequest 中使用的 EventArgs e 值

更改以下内容:

private void HandleUnpackRequest(object sender, EventArgs e) 

对此:

private void HandleUnpackRequest(object sender, UnpackEventArgs e) 

记住事件处理程序声明:

public event EventHandler<UnpackEventArgs> UnpackRequest;