WPF Caliburn.Micro/mvvm Navigation

前端 未结 2 1140
感动是毒
感动是毒 2021-02-09 05:39

I\'m building a project, and one of the biggest problems I\'ve come across until now is navigation.
I\'ve been looking for some time now for examples of caliburn.micro/mvvm

2条回答
  •  孤街浪徒
    2021-02-09 06:23

    Have a read about Conductors and Screens on the official documentation.

    As a simple example, your ShellViewModel could be a Conductor of one active screen (i.e. only one screen becomes active/inactive at a time):

    public class ShellViewModel : Conductor.Collection.OneActive
    

    You can then set the ActiveItem of the Conductor to the view model instance that you wish to be currently active:

    this.ActivateItem(myMainViewModel);
    

    A collection Conductor type also provides an Items collection which you can populate as you instantiate new windows. Viewmodels in this Items collection may be those that are currently deactivated but not yet closed, and you can activate them by using ActivateItem as above. It also makes it very easy to create a menu of open windows by using an ItemsControl with x:Name="Items" in your ShellView.

    Then, to create the ShellView, you can use a ContentControl and set its name to be the same as the ActiveItem property, and Caliburn.Micro will do the rest:

    
    

    You can then respond to activation/deactivation in your MainViewModel by overriding OnActivate/OnDeactivate in that class.

提交回复
热议问题