WindowWorkspace:Managing Windows, Views/Presenters

Topics: CAB & Smart Client Software Factory
Feb 4, 2006 at 5:09 PM
originally posted by: dirtysanchez33

Hi,

I am having problems trying to manage both the Windows (Forms) created by a window workspace as well as the views/presenters that are loaded into these forms.

Here is my scenario:

In my views, I use DI on a property to create a Presenter for my view:

CreateNew
public Presenter Presenter{
set {...}
}

In my WorkItem, I create the view (which automatically creates the presenter) and then show it in a window workspace. My view has an 'OK' button on it. There are two ways the user can close the Window.

1) By clicking the OK button on the view, which I want to close the window that the view is in
2) By clicking the 'X' button on the Window

In scenario 1), I can catch a "OKButtonclicked" event (broadcast from the Presenter) in my WorkItem and remove both the view and presenter (by accessing the view.Presenter property) from the WorkItems.Items collection. But how do I close the popup window since I don't have a direct reference to it. I suppose I could use the view.Parent property to dispose it, but this seems to circumvent the Workspace's management of it's windows, so I don't want to do that. So I am stuck in this scenario.

in scenario 2) I can listen to the Workspace's SmartPartClosing event and remove the view from the workitems.Items collection by using the WorkspaceCancelEventArgs.SmartPart variable like so:

private void Workspace_SmartPartClosing(object sender, WorkspaceCancelEventArgs e)
{

Items.Remove(e.SmartPart);
}

However, in this scenario, I don't have explicit access to the Presenter to remove it. I also don't want to downcast e.SmartPart to my particular view type (so that I could access it's Presenter property) because I may have many different types of views that are popping up in this workitem and I have no way of knowing what smartpart caused the SmartPartClosing event to fire. So in this scenario, I am stuck with my presenter hanging around in my Workitem.Items collection.

Is there a clean way to manage views/presenters within a workitem when using the WindowWorkspace? It seems like theres a huge overhead in the amount of code one must write to manage these... or am I completely missing something.

Thanks!
Feb 4, 2006 at 5:35 PM
originally posted by: dirtysanchez33

Hmmm. Okay, I seem to be making progress here.

To solve scenario 1), I simply dispose the view (from the presenter) which raises the view.OnDipose event. The window workspace is listening to this event and seems to close itself when this happens. So, when the user clicks the 'OK' button, I have my presenter call a custom function on the view called Close() which calls this.Dispose(). So the WindowWorkspace picks up this event and closes the appropriate Form that it is managing in it's collection.

But this means that I have to dispose my view. I may want to keep my view around (if it was expensive to create), but still close the Window that it appears in (my view is defined and created in the workItem and just added to the window workspace). I still don't know how to do this... slowly slowly making progress....

BrianMSFT, any ideas or recommendations for managing Smartparts/Presenters displayed through a windowWorkspace?