Frage

I have a "parent" view containing several regions, into which I load other views. These "child" views are always the same and don't change at runtime, so they are registered with their regions during application startup, using the regionManager.RegisterViewWithRegion... syntax. All the views are currently singletons, and I use Castle Windsor for IoC.

Bearing in mind that 'INavigationAware' methods don't get called on view-models in nested regions, what's a good approach for initialisation and cleanup of my child views? Is it an "acceptable" design for the parent VM to hold references (injected) to the child VM? I could then call some kind of init/cleanup methods on the child VMs from within the parent's OnNavigatedTo()/From() method?

Another option might be to change the views to transient, then I could simply use the constructor for initialisation, and not worry about cleanup (which I only do to clear down old "state" before the view is re-used and displayed). The only potential issue is that my application uses an "event service" (similar to Prism's event broker), with some view-models acting as publishers and others as subscribers. I'm concerned that if I made them transient, the view-models won't get GC'd because of the event subscription, and these "dead" views would continue responding to events. Is this correct? If so, do I presumably need to implement a way of cleaning up (as above), where I can unsubscribe?

War es hilfreich?

Lösung

I've came across the same problem, a while back.

The way I did it was to implement the INavigationAware interface on the Parent ViewModel and in the ViewModels of those scoped Regions/Views and then on the Parent ViewModel OnNavigatedFrom method, call the OnNavigatedFrom of the children ViewModels.

Bear in mind that you have to keep a reference of the Children ViewModels in the Parent ViewModel (which I believe is not a bad practice). This reference can be injected by the Container and deleted (Teared Down) when you navigate away from the Parent View (depending on the value of the KeepAlive property if you've implemented the IRegionMemberLifetime in the parent and children ViewModels).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top