Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Event Notification
Message
 
 
To
01/12/2010 14:23:17
Timothy Bryan
Sharpline Consultants
Conroe, Texas, United States
General information
Forum:
ASP.NET
Category:
Windows Presentation Foundation (WPF)
Environment versions
Environment:
C# 4.0
Application:
Desktop
Miscellaneous
Thread ID:
01491399
Message ID:
01491427
Views:
50
Hi Tim,

I was recently viewing an article on LTD website and here is another link on this topic
http://www.thinqlinq.com/Post.aspx/Title/Reactive-Extensions-responding-to-UI-events

May be it can be relevant to your problem?

Article on LTD http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/exploring-reactive-extensions-adding-a-d



>>>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.....
>
>
>OK, thanks to all.. Just to be sure I understand, if I subscribe to the exact same event and wire it to the exact same handler more than once, it would be basically subscribed twice. And if I unsubscribe it once, then it would still be subscribed because it was subscribed more than the one time.
>
>
>// Creating a ViewModel / View that has the ability to cause trigger another ViewModel / View to be added to the main window
>
>private void ShowPersonSearch()
>{
>	PersonSearchViewModel workspace = new PersonSearchViewModel();
>
>	workspace.WorkspaceNeedsAdded += new WorkspaceNeedsAddedDelegate(workspace_WorkspaceNeedsAdded);
>	this.Workspaces.Add(workspace);
>	this.SetActiveWorkspace(workspace);
>}
>
>// Event Handler to add the workspace to the view when raised by another ViewModel
>private void workspace_WorkspaceNeedsAdded(WorkspaceViewModel workspace, EventArgs e)
>{
>	this.Workspaces.Add(workspace);
>	this.SetActiveWorkspace(workspace);
>}
>
>/// <summary>
>/// Responds to the Workspace requesting to be closed
>/// </summary>
>/// <param name="sender">Sender</param>
>/// <param name="e">Event Args</param>
>private void OnWorkspaceRequestClose(object sender, EventArgs e)
>{
>	WorkspaceViewModel workspace = sender as WorkspaceViewModel;
>
>                     // Does this Workspace raise an event to cause another workspace to be added.
>	if (WorkspaceNeedsAddedUsed)
>		workspace.WorkspaceNeedsAdded -= new WorkspaceNeedsAddedDelegate(workspace_WorkspaceNeedsAdded);
>
>	workspace.Dispose();
>	this.Workspaces.Remove(workspace);
>}
>
>
>In this case the PersonSearchViewModel is created within the MainWindowViewModel and it then subscribes to the event. This is because the Person Search View Model may need to cause another ViewModel to be added to the workspace. When this PersonSearch ViewModel is closed by the user by closing the actual view, I will need to remove the subscription to the event. However the event may also be wired up by another ViewModel in the meantime.
>
>The Handler will act upon the event accordingly because the viewMode is passed in. I just am trying to verify I can always just add a subscription, and remove one when the view is closed and not worry about which ViewModel added it before removing it.
>Thanks
>Tim
If it's not broken, fix it until it is.


My Blog
Previous
Reply
Map
View

Click here to load this message in the networking platform