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 ViewMo开发者_JAVA技巧del.
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?
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.
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:
- Use standard .NET events. Assuming the
ShellViewModel
has a reference to theToolBarViewModel
instance, subscribe to theToolBarViewModel
's event in theShellViewModel
to detect the submit, and pass the new screen instance to the delegate instance. - Use a mediator pattern (such as the
EventAggregator
included in Caliburn.Micro). Subscribe to the event in theShellViewModel
, and publish the event in theToolBarViewModel
- Actually conduct the
ToolBarViewModel
in theShellViewModel
(presumably you'll have to use theConductor<T>.Collection.AllActive
type). TheToolBarViewModel
will then be aScreen
and have aParent
property (yes, this is ugly).
精彩评论