イベント、デリゲート、サブスクライバーについて学んでいます。私は過去 2 日間、調査に費やし、頭脳をすべて包み込みました。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; }
}
}
サブスクライバーを追加するコードと、個々のフォームでプロジェクト コンポーネントを再構成する方法については、まだ作業中です。しかし、私が今取り組もうとしている部分は、渡された引数を使用して 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);
}
助けを得るために必要なすべての部品を含めたと思いますか?! ありがとう!