简体   繁体   English

WPF Caliburn.Micro / mvvm导航

[英]WPF Caliburn.Micro/mvvm Navigation

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 navigation, but they all seem to be really long and I couldn't really understand much of it (beginner here!). 我一直在寻找一些时间用于caliburn.micro / mvvm导航的例子,但它们似乎都很长,我真的不太了解它(初学者在这里!)。

Some info about my project: 关于我的项目的一些信息:
I want there to be an outer window/shell, with menu links/tabs that open pages according to the button clicked inside an inner part of the shell, and be able to open change the page from within a one. 我希望有一个外部窗口/外壳,菜单链接/选项卡根据在shell内部单击的按钮打开页面,并且能够打开从一个内部更改页面。

I currently have: ShellViewModel.cs, MainViewModel.cs, my models, and my views. 我目前有:ShellViewModel.cs,MainViewModel.cs,我的模型和我的观点。 For now, all I need to know is how to make MainViewModel load inside shellviewmodel on startup(using contentcontrol/frames...), and how to move from one page to another. 现在,我需要知道的是如何在启动时使用contentcontrol / frames ...加载MainViewModel并使用contentcontrol / frames ...,以及如何从一个页面移动到另一个页面。

You could also just write it in points, and link me to some useful examples, and I believe I could continue from there. 你也可以用点来写它,并把我链接到一些有用的例子,我相信我可以从那里继续。 It'd be best to get a thorough explanation of stuff if possible . 如果可能的话,最好彻底解释一下这些东西。

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 (ie only one screen becomes active/inactive at a time): 举个简单的例子,你的ShellViewModel可能是一个活动屏幕的Conductor (即一次只有一个屏幕变为活动/非活动状态):

public class ShellViewModel : Conductor<IScreen>.Collection.OneActive

You can then set the ActiveItem of the Conductor to the view model instance that you wish to be currently active: 然后,您可以将ConductorActiveItem设置为您希望当前处于活动状态的视图模型实例:

this.ActivateItem(myMainViewModel);

A collection Conductor type also provides an Items collection which you can populate as you instantiate new windows. 集合Conductor类型还提供了一个Items集合,您可以在实例化新窗口时填充它。 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. Items集合中的Viewmodel可能是当前已停用但尚未关闭的视图模型,您可以使用上述ActivateItem激活它们。 It also makes it very easy to create a menu of open windows by using an ItemsControl with x:Name="Items" in your ShellView . 通过在ShellView使用带有x:Name="Items"ItemsControl ,可以非常轻松地创建打开窗口的菜单。

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: 然后,要创建ShellView ,您可以使用ContentControl并将其名称设置为与ActiveItem属性相同,Caliburn.Micro将完成剩下的工作:

<ContentControl x:Name="ActiveItem" />

You can then respond to activation/deactivation in your MainViewModel by overriding OnActivate / OnDeactivate in that class. 然后,您MainViewModel通过覆盖该类中的OnActivate / OnDeactivate来响应MainViewModel的激活/取消激活。

In ShellView you use a content control like this: 在ShellView中,您使用如下内容控件:

<ShellView xmlns:cal="http://caliburnproject.org/">
     <StackPanel>
           <Button Content="Show other view" cal:Message.Attach="ShowOtherView" />
           <ContentControl cal:View.Model="{Binding Child}" />
     </StackPanel>
</ShellView>

ShellViewModel: ShellViewModel:

public class ShellViewModel : Screen
{
     private object Child;

     public object Child
     {
           get{ return child; }
           set
           {
                if(child == value)
                     return;
                child = value;
                NotifyOfPropertyChange(() => Child);
           }
     }

     public ShellViewModel()
     {
         this.Child = new MainViewModel();
     }

     public void ShowOtherView()
     {
           this.Child = new FooViewModel();
     }
}

So this is a very basic example. 所以这是一个非常基本的例子。 But as you see, your ShellView provides a ContentControl , which shows the child view. 但是如您所见,您的ShellView提供了一个ContentControl ,它显示了子视图。 This ContentControl is bound via View.Model to the Child property from your ShellViewModel. ContentControl通过View.Model绑定到View.ModelChild属性。

In ShellView, I used a button to show a different view, but you can also use a menu or something like that. 在ShellView中,我使用一个按钮来显示不同的视图,但您也可以使用菜单或类似的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM