I am just learning about events, delegates and subscribers. I've spent the last 2 days researching and wrapping my brain around it all. I am unable to access the information being passed in my EventArgs e value. I have a saved project that wants to be opened. The state of the necessary forms are deserialized into a dictionary. A loop is hit that raises the UnpackRequest passing the key/value with it.

ProjectManager.cs file:

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

Then farther down:

ProjectManager.cs file:

//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);
    }
}

Then within the open method, after dictionary gets populated with states of each form:

ProjectManager.cs file:

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

I have a class for 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; }
    }
}

I am still working on the code to add subscribers and how the project components get re-constituted in the individual forms. But the part I'm trying to work on now is in the MainForm.cs where I handle the Unpacked Request using the arguements getting passed. My e contains the key values and the key represents where to send the value (which is the form object).

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);
}

I think I included all the necessary parts to get any help?! THANKS!

有帮助吗?

解决方案

Change this:

private void HandleUnpackRequest(object sender, EventArgs e) 

To this:

private void HandleUnpackRequest(object sender, UnpackEventArgs e) 

Remember your event handler declaration:

public event EventHandler<UnpackEventArgs> UnpackRequest;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top