Possible CloseView bug

Oct 29, 2007 at 9:47 AM
All

I am not sure if this behaviour has been previously observed, but I believe that there is a bug in the SCSF (CAB). When you call CloseView() in a view's presenter, you would expect the view to be completely closed (and Disposed). I noticed that if I closed a view in this fasion, and then later recreated it, although all signs would indicate that you are getting a new instance of the view, in reality, you just appear to get a new reference to the original view. I discovered this by virtue of the fact that I was handling the Views unload event in the prensenter, and found that for however many times I had closed and then recreated the view, I was getting the same number of unload events. E.g. If I closed and created a view three times, I would get three view unloaded events firing.

On looking through the source code, I found that the code responsible for closing the view is in the CloseInternal method of the Workspace class. It closes the view by removing it from it's SmartParts collection, code below:

protected void CloseInternal(TSmartPart smartPart)
{
OnClose((TSmartPart)smartPart);

smartParts.Remove((TSmartPart)smartPart);

if (activeSmartPart != null && activeSmartPart.Equals(smartPart))
{
activeSmartPart = null;
}
}

When you later call ShowViewInWorkspace, the code does appear to not find an instance so add's and returns a new instance of the view. But in reality this is not happening. This is because the workItem belonging to the workspace has it's own SmartParts collection, and when the view is closed, it is not removed from this collection and hence the odd behaviour.

I amended the code above to the following, and this resolved the problem:

protected void CloseInternal(TSmartPart smartPart)
{
OnClose((TSmartPart)smartPart);

smartParts.Remove((TSmartPart)smartPart);
workItem.SmartParts.Remove(smartPart); //new line

if (activeSmartPart != null && activeSmartPart.Equals(smartPart))
{
activeSmartPart = null;
}
}

Does anyone know of any reason why the workItems smartparts collection is not altered currently, or is this merely a bug.

Cheers

Steve
Nov 6, 2007 at 3:49 PM
Edited Nov 7, 2007 at 3:13 AM
I think that this is not the right place to remove the smartpart from the workitem's Smartparts collection.

you should instead terminate the workitem when the view is closed from the presenter or from the parent workitem (or simply remove the smartpart and not terminate the workitem.)

the smartparts collection of the workspace contains the smartparts that the workspace is responsible for, but the smartparts collection of the workitem contains the smartparts (visible or not) that belong to the workitem and not necessary to a specific workspace.

I hope that this help you...