ASP.Net与代表的观察员

时间:2011-08-18 22:34:57

标签: c# asp.net observers

好吧,我在asp.net 3.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结束后,标题回到“你好”......

知道为什么吗?

1 个答案:

答案 0 :(得分:2)

我认为问题是NextButton_Click事件中的“Page”与您将标题设置为“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);
    }
}

希望能为您提供一些问题指示。