Minimum size to connect to Views and ViewModels in ReactiveUI
According to the docs :
The easiest way to use a Location View is through a ViewModelViewHost control, which is a view (on Cocoa, UIView / NSView, and on XAML-based platforms, a control) that has a single ViewModel property. When the ViewModel property is set, the View Location looks for the associated View and loads it into the container.
What I usually do. All your Models are Viewmodels / SubViewmodels, etc. Only have a matching View that implements IViewFor<whateverViewModel>
. I am using WPF and it just means I have to spit in one boiler dependency property ViewModel
, which is good. Then you register with the ReactiveUI IoC container, Splat:
To use the Location view, you must first register the types using the Splat Service Location feature.
Locator.CurrentMutable.Register(() => new ToasterView(), typeof(IViewFor<ToasterViewModel>));
So basically, when your View takes over / multiple controls ViewModelViewHost
on it, once you set or bind a viewmodel to it, it will search and load the registered view. ViewModelViewHost
is the container control that hosts the view.
Views are aware of ViewModels, but ViewModels are not aware of views.
As for the hierarchy, it ViewModelViewHost
will update based on what the ViewModel is attached to and they will turtles all the way down. Usually my top level views are pretty much all controls ViewModelViewHost
and they just expand from there. Using ReactiveUI methods .WhenAny()
you can easily view properties up and down the ViewModels / SubViewModels hierarchy, etc. And don't worry about re-subscribing or null-Checks.
source to share