I'm a bit confused about what might be the right way to close a view:
- Look for the workspace of the view and call IWorkspace.Close(view).
This could for example be done by creating a method in Presenter which calls Presenter.CloseView - the latter is protected and therfore can't be called from outside.
IWorkspace.Close does not return any value indicating if the view has actually been closed. (Close process might have been canceled by SmartPartClosing / e.Cancel=true.)
- Call Presenter.OnCloseView()
In most exampes I've seen, this method is overridden and calls Presenter.CloseView() which calls IWorkspace.Close(view) if the view is still found in a workpace's SmartParts collection.
OnCloseView seems to be called by Presenter.Dispose. So if the workspace disposes the view, OnCloseView will be called again. In that case, IWorkspace.Close won't be called again as the view has already been removed from the Workspace's SmartParts collection.
What is the reason for calling OnCloseView() in Presenter.Dispose?
The method seems to have two purposes which is a bit confusing:
1. To close the view.
2. A handler called when the view is being closed.
When using OnCloseView() you have to be aware of the fact that the view might not have actually been closed, as some handler of SmartPartClosing might have canceled the closing process by setting e.Cancel to true. Therefore, if you have any cleanup code
which should be performed after the view has been closed, you first have to check if the view has actually been closed.
So maybe the Dispose-Method is a better place to perform cleanup actions, right?
- Call View.Dispose()
This will close the view (cannot be canceled by SmartPartClosing e.Cancel=true).
There is IWorkspace.SmartPartClosing event which can be used to cancel the closing process (e.g. because the view has unsaved changes).
But there is no IWorkspace.SmartPartClosed event.
So, there is no notification when the view is actually closed.
You cannot use SmartPartClosing event for that, because there might be additional handlers of the same event which might execute later and set e.Cancel to true.
Is creating a SmartPartClosed event and raising it from within OnClose method of the workspace a good solution?