WPF Caliburn.Micro/mvvm 导航

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16545750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:47:40  来源:igfitidea点击:

WPF Caliburn.Micro/mvvm Navigation

c#wpfmvvmcaliburn.micro

提问by Asaf

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.

关于我的项目的一些信息:
我希望有一个外部窗口/外壳,菜单链接/选项卡根据在外壳内部单击的按钮打开页面,并且能够从一个内部打开更改页面.

I currently have: ShellViewModel.cs, MainViewModel.cs, my models, and my views. 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.

我目前有:ShellViewModel.cs、MainViewModel.cs、我的模型和我的视图。 现在,我需要知道的是如何在启动时在 shellviewmodel 中加载 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.

你也可以把它写成点,并将我链接到一些有用的例子,我相信我可以从那里继续。如果可能的话,最好对事物进行彻底的解释。

回答by devdigital

Have a read about Conductors and Screenson the official documentation.

在官方文档中阅读有关导体和屏幕的信息。

As a simple example, your ShellViewModelcould be a Conductorof one active screen (i.e. only one screen becomes active/inactive at a time):

作为一个简单的例子,您ShellViewModel可以是Conductor一个活动屏幕(即一次只有一个屏幕变为活动/非活动状态):

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

You can then set the ActiveItemof the Conductorto the view model instance that you wish to be currently active:

然后,您可以设置ActiveItemConductor,以你希望成为当前活动视图模型实例:

this.ActivateItem(myMainViewModel);

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

集合Conductor类型还提供了一个Items集合,您可以在实例化新窗口时填充该集合。这个Items集合中的视图模型可能是那些当前已停用但尚未关闭的视图模型,您可以通过使用ActivateItem如上激活它们。这也使得它非常容易使用的创建打开的窗口中的菜单ItemsControlx:Name="Items"你的ShellView

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

然后,要创建ShellView,您可以使用 aContentControl并将其名称设置为与ActiveItem属性相同,Caliburn.Micro 将完成剩下的工作:

<ContentControl x:Name="ActiveItem" />

You can then respond to activation/deactivation in your MainViewModelby overriding OnActivate/OnDeactivatein that class.

然后,您可以MainViewModel通过覆盖该类中的OnActivate/来响应您的激活/停用OnDeactivate

回答by nein.

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:

外壳视图模型:

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. This ContentControlis bound via View.Modelto the Childproperty from your ShellViewModel.

所以这是一个非常基本的例子。但是如您所见,您的 ShellView 提供了一个ContentControl,它显示了子视图。这ContentControl通过您的 ShellViewModel绑定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 中,我使用按钮来显示不同的视图,但您也可以使用菜单或类似的东西。