According to the docs:
The easiest way to use View Location is via the ViewModelViewHost control, which is a View (on Cocoa, a UIView/NSView, and on XAML-based platforms a Control) which has a single ViewModel property. When the ViewModel property is set, View Location looks up the associated View and loads it into the container.
That's what I usually do. All your Viewmodels/SubViewmodels etc. just have a corresponding View that implements IViewFor<whateverViewModel>
. I use WPF, and this just means I have to plop in one boiler-plate ViewModel
dependency property and it is good to go. Then you register with ReactiveUI's IoC container, Splat:
To use View Location, you must first register types, via Splat's Service Location feature.
Locator.CurrentMutable.Register(() => new ToasterView(), typeof(IViewFor<ToasterViewModel>));
So basically whenever your View hosts a/many ViewModelViewHost
control(s) on it, once you set or bind a viewmodel to it, it will look up and load the registered view. ViewModelViewHost
is a container control that hosts a View.
The Views are aware of the ViewModels, but the ViewModels are not aware of the Views.
As far as hierarchy goes, ViewModelViewHost
will update based on whatever ViewModel is bound to it, and they will turtle all the way down. Usually my top-level Views are almost all a bunch of ViewModelViewHost
controls and they just drill-down from there. Using ReactiveUI's .WhenAny()
methods, you can watch properties up and down the ViewModels/SubViewModels etc. hierarchy easily and without having to worry about resubscription or null-checks.