イベント、デリゲート、およびサブスクライバについて学習しています。私は最後の2日間を研究し、私の脳をそれのまわりで包み込みました。 EventArgsのe値で渡される情報にアクセスできません。私は開かれることを望む救済されたプロジェクトを持っています。必要な書式の状態は、辞書にデシリアライズされます。キー/値を渡すUnpackRequestを呼び出すループが発生します。HandleRequestで使用するEventArgs e値にアクセスできない
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; }
}
}
私はまだ加入者を追加するためのコードに取り組んでいますが、どのようにプロジェクトコンポーネントは、個々の形で再構成します。しかし、私が今作業しようとしている部分は、渡された引数を使用してUnpacked Requestを処理する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);
}
私は何か助けを得るために必要なすべての部分が含まれていると思いますか?ありがとう!これに
private void HandleUnpackRequest(object sender, EventArgs e)
:この
をありがとうございました!!それはそれをした! –
Cool。それを「回答済み」とマークしてください。 – AlfredBr