Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Event Notification
Message
From
01/12/2010 13:10:03
 
 
General information
Forum:
ASP.NET
Category:
Windows Presentation Foundation (WPF)
Environment versions
Environment:
C# 4.0
Application:
Desktop
Miscellaneous
Thread ID:
01491399
Message ID:
01491421
Views:
48
>>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....
Of course this means that if you have wired the same eventhandler up twice you need to remove both instances...
>Hope that makes sense.....
Previous
Reply
Map
View

Click here to load this message in the networking platform