开发者

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:

  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).
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜