SCSF - WPF SmartPart getting closed while loading causes Exception

posted Jul 1, 2010, 10:52 AM by io Wint   [ updated Jul 1, 2010, 11:07 AM ]
We had an issue with a view getting closed conditionaly on its load. Its perfect to terminate a view from loading based on some condition. In this case the loading view opens a modal window to get some input to take that decision. 
Issue was not replicable in few systems, which made me wonder why. 

The reason behind the issue is due to the way the internal code of Deckworkspace ( WPF) works. When you look at it, you will notice Deckworkspace.OnShow() getting fired when you try to open a view. Now there is a line in the Onshow() which is to activate the ElementHostWorkspace which contains the smartPart-> the View. Funny thing is, WPF views Loaded event fires before this line in the OnShow() executes! Loaded event calls the _presenter.OnCloseView() which calls the Deckworkspace.OnClose(). OnClose() so awesomely removes the smartPart from its internal collection. Now after all this, the Activate() method call in the OnShow() executes, and there is a ThrowIfSmartPartNotShownPreviously() HEnce you get an exception. 

We had couple of ways of fixing this issue. We removed the possibilities of modifying the internal code and just had the Loaded events logic in a DispatcherTimer which was set to trigger after the load. Thats after the OnShow() method completed, i.e after the Activate() which caused al the issue.


P.S: i am writing it with wat i remember, check for the actual method names. There is a fair chance tat i have written few methods names with their Levinsteins distance >0 from the actual one!!