1

ええと、私はasp.net3.5サイトで働いています。

私は次のようなオブザーバーを設定しました:

public delegate void ActionNotification();

protected Dictionary<string, List<ActionNotification>> Observers
{
    get
    {
        Dictionary<string, List<ActionNotification>> _observers = Session["Observers"] as Dictionary<string, List<ActionNotification>>;
        if (_observers == null)
        {
            _observers = new Dictionary<string, List<ActionNotification>>();
            Observers = _observers;
        }
        return _observers;
    }
    set
    {
        Session["Observers"] = value;
    }
}

public void Attach(string actionName, ActionNotification observer)
{
    if (!Observers.ContainsKey(actionName))
    {
        Observers.Add(actionName, new List<ActionNotification>());
    }
    Observers[actionName].Add(observer);
}

public void Detach(string actionName, ActionNotification observer)
{
    if (Observers.ContainsKey(actionName))
    {
        Observers[actionName].Remove(observer);
    }

}
public void DetachAll(string actionName)
{
    if (Observers.ContainsKey(actionName))
    {
        Observers.Remove(actionName);
    }
}

public void Notify(string action)
{
    if (Observers.ContainsKey(action))
    {
        foreach (ActionNotification o in Observers[action])
        {
            o.Invoke();
        }
    }
}

私は次のようにオブザーバーを使用します:

//Esta es llamada al notify con cierto action       
protected void btnNext_Click(object sender, ImageClickEventArgs e)          
{           
    Notify("Next");         
} 
//Y este es el register del Listener            
Attach("Next", new ActionNotification(NextButton_Click)); 

前の場合o.Invoke(); たとえば、ページタイトルを「Hello」に変更します。そして、NextButton_Click「さようなら」に設定した「」の中で、NextButton_Clickフィニッシュ後、タイトルは「ハロー」に戻ります。

なぜですか?

4

1 に答える 1

2

NextButton_Click問題は、イベントの「ページ」が、タイトルを「Hello」に設定したページと同じページではないことだと思います。イベントが発生したときにセッション内のイベントを渡しているため、オブジェクトが作用する範囲はなくなりました。次のコードで再作成できます (これは EventHandlers を使用していますが、基本的にはコードで概説したものと同じです)

protected void Page_Load(object sender, EventArgs e)
{
    this.Page.Title = "test";

    //Store it in your session...seems like a weird thing to do given how your page should be stateless, so I would think about what you are
    //trying to do a bit more carefully.  You don't want to call an event handler such as test below from another page in your asp.net app.
    Dictionary<string, EventHandler> myEvents = null;
    if (Session["Invokers"] == null)
    {
        myEvents = new Dictionary<string, EventHandler>();
        Session["Invokers"] = myEvents;
    }
    else
    {
        myEvents = Session["Invokers"] as Dictionary<string, EventHandler>;
    }
    //If the event handler key is not in there then add it
    if (myEvents.ContainsKey("buttonClickOnPageDefault") == false)
    {
        //Subscribe to event (i.e. add your method to the invokation list
        this.TestEvent += new EventHandler(test);
        myEvents.Add("buttonClickOnPageDefault", this.TestEvent);
    }
    else
    {
        //if it does contain this key then you may already be subscribed to event, so unsubscribe in case and then resubscribe...you could
        //probably do this more elegantly by looking at the vales in the GetInvokationList method on the eventHandler
        //Wire up the event
        this.TestEvent -= new EventHandler(test);
        this.TestEvent += new EventHandler(test);
    }
    //Resave the dictionary.
    Session["Invokers"] = myEvents;
}

void test(object o, EventArgs e)
{
    this.Page.Title = "testEvent";
}

public event EventHandler TestEvent;

protected void Button1_Click(object sender, EventArgs e)
{
    if (Session["Invokers"] != null)
    {
        Dictionary<string, EventHandler> myEvents = (Dictionary<string, EventHandler>)Session["Invokers"];
        if (myEvents.ContainsKey("buttonClickOnPageDefault"))
        {
            EventHandler ev = myEvents["buttonClickOnPageDefault"];
            ev(null, EventArgs.Empty);
        }
    }
}

上記のコードを asp.net ページに配置すると、ページ タイトルが変更されることはありませんが、Testメソッドにブレークポイントを配置すると、ヒットすることがわかります。その理由は、別のページでヒットしているためです (そのページは範囲外であり、イベントがまだ参照しているためガベージ コレクションされない可能性があるため、メモリ リークが発生する可能性があります...注意してください! )。実際には、おそらくこの方法でイベントを使用するべきではありません (少なくともページで動作しないように...おそらく、ドメイン オブジェクトに対する何らかのユーティリティがあるからです)。以下が機能することに注意してください(同じページで動作するため)

protected void Page_Load(object sender, EventArgs e)
{
    this.Page.Title = "test";

    //Store it in your session...seems like a weird thing to do given how your page should be stateless, so I would think about what you are
    //trying to do a bit more carefully.  You don't want to call an event handler such as test below from another page in your asp.net app.
    this.TestEvent += new EventHandler(test);
    Session["Invoker"] = this.TestEvent;
}

void test(object o, EventArgs e)
{
    this.Page.Title = "testEvent";
}

public event EventHandler TestEvent;

protected void Button1_Click(object sender, EventArgs e)
{
    if (Session["Invoker"] != null)
    {
        EventHandler ev = (EventHandler)Session["Invoker"];
        ev(null, EventArgs.Empty);
    }
}

あなたの問題がどこにあるのかを示すヒントが得られることを願っています。

于 2011-08-19T00:02:06.530 に答える