I have a ViewModel that can create another ViewModel.
I want to set this ViewModel as the data context of existing View, then Navigate to that View.
In MyViewModel
private void CreateNewOrder()
{
var order = new OrderViewModel(new Order(){/* setup some things */});
// another ViewModel subscribes to MyCustomEvent
this.eventAggregator.GetEvent<MyCustomEvent>().publish(order);
this.regionManager.RequestNavigate("DetailRegion", new Uri("OrderView", UriKind.Relative));
}
The problem is …
- My ViewModel needs reference to the region’s name, and the View’s name.
- “DetailRegion” must contain the “OrderView” object.
- DetailView.DataContext is set to OrderViewModel (using aggregate event subscription)
Is this tight coupling?
I’ve spent sometime looking in the Prism Quickstart project and found the “Controller” or something that is too complicated.
I just want to simplify decoupling View-ViewModel.
The code you have added to the ViewModel, belongs in a controller.
So what’s missing from this picture?
– Controllers publish and listen for events and fetch data for the
ViewModel from a Model. They also control navigation.
It really should have been designed as MVCVM. I see no end of misuse of MVVM through the absence of controller classes. Give the Prism Quickstart another go.
*Note: VMs and views do not need to hang around waiting for messages, so only controllers actually need to exist for the lifetime of the app (i.e. very small footprint). This is great for mobile apps and generally a good thing for any app.