0

Is there a way whereby I can identify a ChildViewModel instance, which was created by prism's ViewModelLocator, upon opening it's corresponding window?

I would like to trigger that the ChildViewModel should load it's data, based on parameters originating from the MasterViewModel.

In code, in MasterViewModel has an ICommand in a which is responsible for requesting opening a new child window by publishing an event, and there is a corresponding subscriber.

public ICommand OpenNewChildWindow()
{
    Publish(new OpenNewChildWindowPubSubEvent());

    // Maybe I can publish a new PubSubEvent here
    // but how can I target just the recently created ChildViewModel?
}

Notice that the MasterViewModel knows nothing about the UI implementation.

The the subscriber calls ShowWindow method on a custom WindowManager which basically resolves the View (Window in this instance) which corresponds to the ViewModel which was passed in.

public void ShowWindow(Type viewModelType)
{
   Type view = ResolveView(viewModelType);
   Window w = (Window)Activator.CreateInstance(view);
   w.Show();
}

The xaml for the window the appropiate

ViewModelLocator.AutoWireViewModel="True"
softbear
  • 485
  • 6
  • 16
  • This is why you should never use these "locator" things. They save you from the nearly imperceptible discomfort of spending five minutes learning the basics of MVVM, and condemn you to the misery of a lifetime of doing MVVM according to rules set down by somebody who knew nothing whatsoever about it. – 15ee8f99-57ff-4f92-890c-b56153 Jun 07 '17 at 18:41
  • "the misery of a lifetime of doing MVVM according to rules set down by somebody" - good point – softbear Jun 07 '17 at 19:13

1 Answers1

2

Go for a view model-first style of navigation. If you pass a (child-)view model instance (instead of a type) to ShowWindow, you can create that with whatever data you need.

Probably, you pass the data around as payload of the OpenNewChildWindowPubSubEvent, and the subscriber then creates the view model. Or you create the view model immediately in the command and pass that on as payload of the event.

Anyway, don't resolve the view type from the view model type just to resolve the view model type from the view :-)

BTW, the ViewModelLocator is great and really simplifies things, but you don't want to use it here, because you're not navigating within one shell, but creating new windows. If you would, your view model would implement INavigationAware and you'd pass the data to the child view model as parameter to RequestNavigate...

Haukinger
  • 10,420
  • 2
  • 15
  • 28
  • Yes, I guess I have to go back to creating the VM instances myself. I thought that PRISMs ViewModelLocator saved me the hassle of the DI part, guess I'm back to a ServiceLocator-ish pattern to help create VMs and inject dependencies. – softbear Jun 07 '17 at 19:20
  • Add a factory that receives all the dependencies, so that its `CreateMyViewModel` method only has parameters for the real data. – Haukinger Jun 07 '17 at 20:35