Kiwi Ingenuity

Thoughts from another perspective

SCSF - View Removal and Disposal

There are instances where on closing a Work Item View, you would also like to dispose of the footprint.  Here is how it done.

Change the OnCloseView of the Presenter as follows:

#region Closing the View
/// <summary>
/// Close the view
/// </summary>
public void OnCloseView()
{
   base.CloseView();

   // If this presenter has created any workspaces
   // be sure to remove them first.
   // e.g. if (WorkItem.Workspaces.Contains(WorkSpaceName))
   //         WorkItem.Workspaces.Remove(WorkItem.Workspaces[WorkSpaceName]);

   // Remove the View from the SmartParts Collection
   this.WorkItem.SmartParts.Remove(View);

   // As we are closing and re-creating this view
   // several times, we need to Dispose the view.
   if (View is IDisposable)
      ((IDisposable)View).Dispose();

}
#endregion

Open the code behind the  that the presenter controls and add the following Code snippet.

#region Closing the View
void ParentForm_FormClosed(object sender, FormClosedEventArgs e)
{
   _presenter.OnCloseView();
}
#endregion

Finally on the same page change the OnLoad Method so it reads:

protected override void OnLoad(EventArgs e)
{
   _presenter.OnViewReady();
   base.OnLoad(e);

   // Capture the Form Close Event, because we want to destroy 
   // the view on Closing the form

   this.ParentForm.FormClosed += new FormClosedEventHandler(ParentForm_FormClosed);
}

 

And that’s it.   Each time you close the view it will be destroyed and the memory released.

Comments (2) -

Loading