Reusing a View - CommandHandler Issues

Topics: CAB & Smart Client Software Factory
Mar 28, 2007 at 5:54 PM
Edited Mar 28, 2007 at 5:56 PM
I've got the typical scenario going here: a view with a presenter being displayed in a deckworkspace on my shell form. The view itself is displaying an image and the presenter has a few commandhandlers to modify the display of this image for the user.

Now the problem... I'm trying to allow the user to open the view in a dialog to get the 'full screen' effect so that they can get a better look at the image. To reuse that view, I've created a dialog, added a deckworkspace to it and added the appropriate toolbarbuttons to fire off the same display modification commands for the user. Objectbuilder creates the dialog, then I add a new version of the view to it. The problem is I have the same view active twice and they're both listening to the same commands and the image gets update on both views.

How do I isolate each of the views so that the appropriate view is modified when a command is fired from its corresponding window? This wouldn't be too much of an issue if I weren't later going to make that fullscreen dialog available on a search feature. Then my shell form view would be displaying the wrong image altogether.
Mar 29, 2007 at 12:24 AM
Wouldn't the problem go away of you put the command handlers in the presenter for each view, instead of having them in the view itself?
Mar 29, 2007 at 2:07 PM
That's exactly where the commandhandlers are, in the presenter.
Mar 29, 2007 at 3:30 PM
I think that CAB is working as intended. You are invoking command "X" and any item in the container listening to for command "X" will be invoked. Since both views are in the same container(Workitem) they both handle the event. If you put View 1 and the Full screen views into different workitems and used the Subscribe and Publish Event mechanism then you could control the execution by limiting scope on the EventSubscription.

Mar 29, 2007 at 3:58 PM
Well yes, I agree that it is working as intended.

Think about this: I'm using commandhandlers and I have tried putting the 'full screen' view into a separate workitem thinking that's how I could get my commands isolated, but it didn't work, when a command is fired any commandhandler listening will run, across workitems. I've thought about making the commandhandlers publish events to get the EventSubscription only for my workitem like you described above, but that seems a bit clunky, and gets even more so when you consider the amount events that will have to be created.

How about this? Is there a way to limit a command to only be fired or handled inside a new workitem, like you can with an EventSubscription?
Mar 29, 2007 at 4:51 PM
"Is there a way to limit a command to only be fired or handled inside a new workitem" For me this would be a last resort.

Could you use only one Subscription/Publication for all the toolbarbuttons and use a DataEventArgs with and Enum so that the presenter could call the correct method on the view.
Mar 29, 2007 at 6:18 PM
Edited Mar 29, 2007 at 8:47 PM
Is there a way to associate an event with a ToolStripButton like you would a command? If not, then the ToolStripButton would fire the command, then the command would have to fire and event, like so:

<EventPublication(Constants.QueueChangedEvent, PublicationScope.WorkItem)> _
Public Event RotateImage As EventHandler(Of EventArgs)

<CommandHandler()> _
Public Sub RotateImageClicked(byval sender as Object, byval e as EventArgs)
End Sub

Protected Sub OnRotateImage(byal e as EventArgs)
RaiseEvent RotateImage(me, e)
End Sub

To even use eventsubscription/publication I would have to do this for every command, inside the presenter.
Mar 29, 2007 at 9:40 PM

Where are you adding the ToolbarButtons? If the ModuleController is adding the buttons at runtime you might have a hard time separating the two views. If you are adding the buttons at design time then I think my solution could work. Here is what I was thinking. Sorry, this is not correct VB syntax

<EventPublication(Constants.QueueChangedEvent, PublicationScope.WorkItem)> _
Public Event ChangeImage As EventHandler(Of DataEventArgs<TypeOfChange>)