1
我只是了解事件,委托和订阅者。我花了最后2天研究和包装我的大脑。我无法访问在我的EventArgs e值中传递的信息。我有一个想要打开的保存项目。必要形式的状态被反序列化成字典。一个循环被触发,引发UnpackRequest传递键值。无法访问EventArgs e值以在HandleRequest中使用
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);
}
}
然后打开的方法中,后的字典被填充与每个状态格式:
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中,我使用争论通过了处理解压请求。我的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);
}
我想我包含了所有必要的部分以获得任何帮助?谢谢!
太感谢你了!那样做了! – 2012-04-13 20:44:15
很酷。将其标记为“已回答”。 – AlfredBr 2012-04-18 17:03:35