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
ShellViewModelhas a reference to theToolBarViewModelinstance, subscribe to theToolBarViewModel's event in theShellViewModelto detect the submit, and pass the new screen instance to the delegate instance. - Use a mediator pattern (such as the
EventAggregatorincluded in Caliburn.Micro). Subscribe to the event in theShellViewModel, and publish the event in theToolBarViewModel - Actually conduct the
ToolBarViewModelin theShellViewModel(presumably you'll have to use theConductor<T>.Collection.AllActivetype). TheToolBarViewModelwill then be aScreenand have aParentproperty (yes, this is ugly).
加载中,请稍侯......
精彩评论