closing nested user controls

Topics: Questions
Sep 14, 2013 at 6:24 PM
Hello again, I wanna start with a big thank you for your help so far..
..and continue with a new question:
I have a page containing my Batches on the left and the details for the selected one on the right.
Each batch has a number of 'RequestGroups' each containing a Request, Production and Uses. Each Request has a list of RequestedBottleType, and same for the Production (ProducedBottleType instead). I'm not going to go into the Uses as for the example that follows there aren't any.
Everything 'loads', 'modifies', 'saves' fine. My problem comes when unloading(in this example I'm navigating to another page, but the nature of the problem is the same when selecting a different batch).
The problem isn't visible to the user but on the debugger window I see things that seem quite wrong: starting the navigation process, the BatchViewModel is unloaded, and then reloaded with a null context- this causes a number of errors caused by the view's null bindings, then the first child is unloaded (RequestGroupsViewModle), that in turn unloads it's children but not before reloading them with a null context(some more errors). Finally after everything is unloaded, the BatchesPage is reloaded and subsequently the BatchViewModel get's constructed with the SelectedBatch and in turn constructing all the children anew. Then everything is once again unloaded(same story as before) and finally we are free to navigate to the new page.
A few things worth mentioning: I'm still using ModernUI for the navigation. In my views I have a setup for dealing with the case where the binding target for the user control is null, where instead I would show a 'add new' button(say for a Production) based on a ControlTemplate with DataTrigger, so as to not have to load a ProductionViewModel with a null context. Are there any good practices for dealing with the null context scenario(with an example such as Request.Production==null)?
I'm sorry for what follows is quite long but I hope it would shed some light for you because I'm completely in the dark here :)
  • My writeLines, in the form "Title + closing: Model.ID " are from the close() method in the VM's(excl. the ***Bottles VMs)
Sep 14, 2013 at 6:30 PM
Navigating from '/Pages\details\BatchesPage.xaml' to '/Pages\overview\StockPage.xaml'
04:32:39:343 => [DEBUG] [Catel.Windows.Controls.MVVMProviders.Logic.LogicBase] Target control 'BatchesPage' is unloaded
04:32:39:360 => [DEBUG] [Catel.MVVM.Views.ViewManager] Unregistering view 'Sklad.UI.Pages.details.BatchesPage'
04:32:39:363 => [DEBUG] [Catel.MVVM.Views.ViewManager] Unregistered view 'Sklad.UI.Pages.details.BatchesPage'
04:32:39:373 => [DEBUG] [Catel.MVVM.Views.ViewToViewModelMappingHelper] Initializing view model 'null'
04:32:39:374 => [DEBUG] [Catel.MVVM.Views.ViewToViewModelMappingHelper] Uninitializing view model 'BatchesViewModel'
04:32:39:375 => [DEBUG] [Catel.MVVM.Views.ViewToViewModelMappingHelper] Uninitialized view model 'BatchesViewModel'
04:32:39:376 => [DEBUG] [Catel.MVVM.Views.ViewToViewModelMappingHelper] Initialized view model 'null'
04:32:39:378 => [DEBUG] [Catel.Windows.Controls.MVVMProviders.Logic.UserControlLogic] Unsubscribed from parent view model
04:32:39:380 => [DEBUG] [Catel.MVVM.Views.ViewToViewModelMappingHelper] Uninitializing view model container to manage ViewToViewModel mappings
04:32:39:381 => [DEBUG] [Catel.MVVM.Views.ViewToViewModelMappingHelper] Uninitialized view model container to manage ViewToViewModel mappings
04:32:39:382 => [DEBUG] [Catel.Windows.Controls.MVVMProviders.Logic.LogicBase] Target control 'BatchView' is unloaded
04:32:39:383 => [DEBUG] [Catel.MVVM.Views.ViewManager] Unregistering view 'Sklad.UI.Views.Batches.BatchView'
04:32:39:384 => [DEBUG] [Catel.MVVM.Views.ViewManager] Unregistered view 'Sklad.UI.Views.Batches.BatchView'
04:32:39:388 => [DEBUG] [Catel.MVVM.ViewModelManager] Unregistering all models of view model 'BatchViewModel' (id = '4')
04:32:39:389 => [DEBUG] [Catel.MVVM.ViewModelManager] Unregistered all '1' models of view model 'BatchViewModel' (id = '4')
Batch View Model closing: 2


04:33:25:069 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Unregistering commands on view model 'Sklad.UI.ViewModels.Controls.Batches.BatchViewModel' with unique identifier '4'
04:33:25:117 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Unregistered commands on view model 'Sklad.UI.ViewModels.Controls.Batches.BatchViewModel' with unique identifier '4'
04:33:25:144 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Disposing commands on view model 'Sklad.UI.ViewModels.Controls.Batches.BatchViewModel' with unique identifier '4'
04:33:25:148 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Disposed commands on view model 'Sklad.UI.ViewModels.Controls.Batches.BatchViewModel' with unique identifier '4'
Sklad.UI.vshost.exe Information: 0 : 04:33:25:150 => [INFO] [Catel.MVVM.ViewModelBase] Closed view model 'Sklad.UI.ViewModels.Controls.Batches.BatchViewModel'
04:33:25:166 => [DEBUG] [Catel.MVVM.ManagedViewModel] Removed view model instance, currently containing '0' instances of type 'Sklad.UI.ViewModels.Controls.Batches.BatchViewModel'
04:33:25:167 => [DEBUG] [Catel.Windows.Controls.MVVMProviders.Logic.UserControlLogic] Unsubscribed from parent view model
04:33:25:168 => [DEBUG] [Catel.MVVM.Views.ViewToViewModelMappingHelper] Initializing view model 'null'
04:33:25:169 => [DEBUG] [Catel.MVVM.Views.ViewToViewModelMappingHelper] Uninitializing view model 'BatchViewModel'
04:33:25:171 => [DEBUG] [Catel.MVVM.Views.ViewToViewModelMappingHelper] Uninitialized view model 'BatchViewModel'
04:33:25:172 => [DEBUG] [Catel.MVVM.Views.ViewToViewModelMappingHelper] Initialized view model 'null'


System.Windows.Data Error: 23 : Cannot convert '<null>' from type '<null>' to type 'System.Double' for 'en-US' culture with default conversions; consider using Converter property of Binding. NotSupportedException:'System.NotSupportedException: DoubleConverter cannot convert from (null).
.....Many more errors of the sort



04:33:25:660 => [DEBUG] [Catel.MVVM.Views.ViewToViewModelMappingHelper] Uninitializing view model container to manage ViewToViewModel mappings
04:33:25:662 => [DEBUG] [Catel.MVVM.Views.ViewToViewModelMappingHelper] Uninitialized view model container to manage ViewToViewModel mappings
04:33:25:663 => [DEBUG] [Catel.Windows.Controls.MVVMProviders.Logic.LogicBase] Target control 'RequestGroupView' is unloaded
04:33:25:663 => [DEBUG] [Catel.MVVM.Views.ViewManager] Unregistering view 'Sklad.UI.Views.Batches.RequestGroupView'
04:33:25:664 => [DEBUG] [Catel.MVVM.Views.ViewManager] Unregistered view 'Sklad.UI.Views.Batches.RequestGroupView'
04:33:25:665 => [DEBUG] [Catel.MVVM.ViewModelManager] Unregistering all models of view model 'RequestGroupViewModel' (id = '6')
04:33:25:666 => [DEBUG] [Catel.MVVM.ViewModelManager] Unregistered all '0' models of view model 'RequestGroupViewModel' (id = '6')
RequestGroup View model closing: 2
....
skipped some more unloading of controls
....
04:45:31:469 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Unregistering commands on view model 'Sklad.UI.ViewModels.Controls.Batches.Uses.UsesViewModel' with unique identifier '10'
04:45:31:481 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Unregistered commands on view model 'Sklad.UI.ViewModels.Controls.Batches.Uses.UsesViewModel' with unique identifier '10'
04:45:31:511 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Disposing commands on view model 'Sklad.UI.ViewModels.Controls.Batches.Uses.UsesViewModel' with unique identifier '10'
04:45:31:523 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Disposed commands on view model 'Sklad.UI.ViewModels.Controls.Batches.Uses.UsesViewModel' with unique identifier '10'
Sklad.UI.vshost.exe Information: 0 : 04:45:31:537 => [INFO] [Catel.MVVM.ViewModelBase] Closed view model 'Sklad.UI.ViewModels.Controls.Batches.Uses.UsesViewModel'
04:45:31:563 => [DEBUG] [Catel.MVVM.ManagedViewModel] Removed view model instance, currently containing '0' instances of type 'Sklad.UI.ViewModels.Controls.Batches.Uses.UsesViewModel'
04:45:31:564 => [DEBUG] [Catel.MVVM.Views.ViewToViewModelMappingHelper] Initializing view model 'null'
04:45:31:566 => [DEBUG] [Catel.MVVM.Views.ViewToViewModelMappingHelper] Uninitializing view model 'UsesViewModel'
04:45:31:567 => [DEBUG] [Catel.MVVM.Views.ViewToViewModelMappingHelper] Uninitialized view model 'UsesViewModel'
04:45:31:568 => [DEBUG] [Catel.MVVM.Views.ViewToViewModelMappingHelper] Initialized view model 'null'
04:45:31:569 => [DEBUG] [Catel.MVVM.Views.ViewToViewModelMappingHelper] Uninitializing view model container to manage ViewToViewModel mappings
04:45:31:570 => [DEBUG] [Catel.MVVM.Views.ViewToViewModelMappingHelper] Uninitialized view model container to manage ViewToViewModel mappings
??????????????????????
04:45:31:572 => [DEBUG] [Catel.Windows.Controls.MVVMProviders.Logic.LogicBase] Target control 'BatchesPage' is loaded
04:45:31:573 => [DEBUG] [Catel.MVVM.Views.ViewManager] Registering view 'Sklad.UI.Pages.details.BatchesPage'
04:45:31:574 => [DEBUG] [Catel.MVVM.Views.ViewManager] Registered view 'Sklad.UI.Pages.details.BatchesPage'
04:45:31:575 => [DEBUG] [Catel.MVVM.Views.ViewToViewModelMappingHelper] Initializing view model container to manage ViewToViewModel mappings
04:45:31:575 => [DEBUG] [Catel.MVVM.Views.ViewToViewModelMappingHelper] Initializing view model 'BatchesViewModel'
04:45:31:576 => [DEBUG] [Catel.MVVM.Views.ViewToViewModelMappingHelper] Initialized view model 'BatchesViewModel'
04:45:31:577 => [DEBUG] [Catel.MVVM.Views.ViewToViewModelMappingHelper] Initialized view model container to manage ViewToViewModel mappings
04:45:31:578 => [DEBUG] [Catel.Windows.Controls.MVVMProviders.Logic.LogicBase] Target control 'BatchView' is loaded
...
in batch ctor

....
unloading..
....
loading new page..
..
Sep 15, 2013 at 6:02 PM
I've come across this "Important Note" on the ModernUI doc site while looking into the navigation events:
Important: do not rely on WPF Loaded and Unloaded events for content initialization and clean up. The Loaded and Unloaded events are raised multiple times when the active content changes. This is due to the use of multiple ContentPresenters in the TransitioningContentControl required for fluent animations. Use OnNavigatedTo and OnNavigatedFrom instead.
Coordinator
Sep 17, 2013 at 7:36 AM
I think this only happens when you have a "detail view model" with an empty ctor. Then the datacontext changes and Catel tries to create a new view model. If you make sure that it can only create view models with actual modes (thus model (datacontext) injection), then this behavior won't happen.
Sep 17, 2013 at 8:08 AM
None of my VM's, except for the topmost, have an empty constructor.
Sep 20, 2013 at 5:03 PM
I haven't yet found the cause for the issue, but in the meanwhile have been working on other bits of the application where I reuse most of the user controls from that 'most detailed' view and the issue doesn't seem to appear. So I feel that sooner or later I'll route this out.