次の方法で共有


Activating WorkItems in a TabWorkspace

Apologies for those to whom the Smart Client Software Factory means nothing, but I thought I'd share a recent experience with regard to the TabWorkspace workspace control.  I do plan to come back and explain SCSF at a more fundamental level in later posts, but hopefully this will prove useful to anyone who has had similar issues.

My problem related to automatically activating SmartParts and their associated WorkItems hosted in a TabWorkspace.  Controls tagged with the [SmartPart] attribute automatically activate their associated WorkItem when activated, but unfortunately switching to a tab hosting the control is not enough to fire this event.  In my particular situation I had a status bar which I wanted updated with information from the active WorkItem, so I needed a way to capure that TabWorkspace control's SelectedIndexChanged event and activate the correct WorkItem.

Whilst the TabWorkspace control does have an ActiveSmartPart property, SmartParts don't contain any special properties for getting at their associated WorkItem.  Given I knew the exact type of SmartPart I was going to be opening in the tab pages I could have simple casted to the appropriate type and got at the WorkItem that way, but this would have introduced tighter coupling and lack of reusability.  Instead I decided to keep a register of tab pages and associated WorkItems, so I created a Dictionary<TabPage, WorkItem> collection in my ShellLayoutView class.  I then used the Event Publication/Subscription model to fire an event when a WorkItem was created, and then used that event to register the WorkItem against the TabPage it appeared in.

 [EventSubscription(EventTopicNames.RegisterWorkItem)]
public void RegisterWorkItem(object sender, EventArgs workItem)
{
    _registeredWorkItems[_rightWorkspace.SelectedTab] = workItem.Data;
}

Finally within the WorkItemController itself I added handlers for the WorkItem.Activated and WorkItem.Terminating events, to update the status bar of the shell application when the active WorkItem changed.  I'm still figuring all the intricasies of CAB/SCSF out and this may not have been the optimal solution, but it worked for me.  I'd love to hear from anyone who found better or alternative ways of achieving the same.