开发者

Can an event be used as an event listener?

I'm trying to expose some events from a private object which is contained inside the object I am creating and it looks like the compiler is happy with this:

private WindowUpdateServer _windowUpdateServer;

public event WindowUpdateHandler WindowUpd开发者_JAVA技巧ated;

public RecieveWindowFramesManager() {

    _windowUpdateServer = new WindowUpdateServer();

    _windowUpdateServer.ExistingWindowUpdated += WindowUpdated; // ExistingWindowUpdated is a WindowUpdateHandler
}

But after RecieveWindowFramesManager is initialized _windowUpdateServer.ExistingWindowUpdated == null.

Am I missing something here - it seems this should work?

It is worth noting that after RecieveWindowFramesManager is initialized I attach an event listener to WindowUpdated but it never gets called (even though _windowUpdateServer.ExistingWindowUpdated gets fired).


I'm not sure, but I think this only assigns those handlers from the WindowUpdated event, that were set when the += operation took place. Since it is a constructor, the list was empty. To do what you want, create your own event handler for ExistingWindowUpdated and fire the WindowUpdated event from there.


I'd do something like this:

public event WindowUpdateEventHandler WindowUpdated
{ 
    add{ _windowUpdateServer.ExistingWindowUpdated += value; }
    remove{ _windowUpdateServer.ExistingWindowUpdated -= value; }
}

It has some issues if you're actually using the sender argument in the event handlers, though; it'll point to the inner object rather than the one you're getting the events from.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜