General information
Category:
Windows Presentation Foundation (WPF)
>Hi All,
>
>This is probably not specifically a WPF question but I am working in WPF at the moment. I will take a stab at explaining what I am trying to do.
>
>I have a main window where I am adding user controls (views) dynamically. I am using an MVVM pattern and thus every view has a ViewModel class. The Main Window View Model keeps track of each view that is added dynamically and when it is closed and these are kept in a collection. Since I have the need to add a view from another view such as one form calling another form I need to notify the main window View Model via an event that I want to add another view. This event is implemented in a base class. so all views have the ability to raise the event notifying the main window ViewModel to add that view.
>
>Here is my question. If I add a view that can raise the event, I subscribe to the event at that time. But when the view is closed or removed from the collection I would need to unsubscribe to the event. This event is sort of generic so can it be subscribed multiple times? If I load a view and subscribe to the event, then another view is loaded and the event is subscribed again. Is this subscription additive? If I remove a view and unsubscribe, will it still be listening to the second view that also subscribed.
I see Bonnie & Mike have both answered but it is useful to understand what is happening when you wire up an event in .NET:
When you execute 'SomeEvent += new Handler(eventhandler)' the address pointer of 'eventhandler' is passed to the event.
(If you inspect the result of the event.GetInvocationList() after wiring up the handler you will see that each listener has a _methodPtr property which stores this value.)
When you execute 'SomeEvent -= new Handler(eventhandler)' the same address will be supplied to the event and, consequently, the correct listener will be removed from the invocation list....
Hope that makes sense.....
Previous
Next
Reply
View the map of this thread
View the map of this thread starting from this message only
View all messages of this thread
View all messages of this thread starting from this message only