Neil Barnwell
Neil Barnwell

Reputation: 42125

Caliburn.Micro - How to ActivateItem from class other than the Conductor

I've composed the UI of my Caliburn.Micro application such that toolbar buttons have their own View and ViewModel.

In the examples I've seen, to activate a new screen, a method in the shell calls Conductor.ActivateItem, passing in the viewmodel instance, however in my case the ToolbarButtonViewModel.Submit() method is on a class that isn't the shell.

What's the best way of activating screens from a class other than the shell?

Upvotes: 3

Views: 2623

Answers (2)

EisenbergEffect
EisenbergEffect

Reputation: 3999

You should consider using the EventAggregator. Create a message which the main conductor handles and send it from the tool bar vm. When the message is recieved by the main conductor, activate the screen that it specifies.

Upvotes: 14

devdigital
devdigital

Reputation: 34349

This sounds like a case for inter-viewmodel communication (between the toolbar view model, and the shell viewmodel). Depending on how loosely coupled you wish the code to be, you could either:

  1. Use standard .NET events. Assuming the ShellViewModel has a reference to the ToolBarViewModel instance, subscribe to the ToolBarViewModel's event in the ShellViewModel to detect the submit, and pass the new screen instance to the delegate instance.
  2. Use a mediator pattern (such as the EventAggregator included in Caliburn.Micro). Subscribe to the event in the ShellViewModel, and publish the event in the ToolBarViewModel
  3. Actually conduct the ToolBarViewModel in the ShellViewModel (presumably you'll have to use the Conductor<T>.Collection.AllActive type). The ToolBarViewModel will then be a Screen and have a Parent property (yes, this is ugly).

Upvotes: 1

Related Questions