首页 > 解决方案 > 在 Caliburn Micro 中的 ViewModel 之间切换

问题描述

我正在尝试使用 Caliburn Micro 制作 WPF 应用程序。我在整页上有一个带有 ContentControl 的 ShellView。我已经在 ShellView 的 ContentControl 启动时显示了一个 UserControl(基本上它是一个登录页面)。登录后,我想关闭当前的 ViewModel 并在 ShellView 的 ContentControl 中显示另一个。我怎样才能做到这一点?

标签: c#wpfmvvmcaliburn.micro

解决方案


您需要首先从 Conductor 类继承 ShellViewModel,从 Screen 继承其他 ViewModel(Login 和 SecondViewModel)。您可以在屏幕和导体上阅读更多信息。例如,

 public class ShellViewModel:Conductor<Screen> 

 public class UserControl1ViewModel: Screen 

 public class UserControl2ViewModel: Screen

ShellViewModel 将在不同的屏幕之间进行,并继承自 Caliburn.Micro 的 Conductor 类。当您显示屏幕时,指挥会确保它已正确激活。如果您正在从屏幕过渡,它会确保它被停用。

您需要进行的第二个更改是在 ShellView 中的上下文控件中,方法是将其绑定到 Conductor 的活动项。

<ContentControl x:Name="ActiveItem"/>

最后,您可以使用 Conductor 的 ActivateItem 方法在屏幕之间切换。

 ActivateItem(new UserControl2ViewModel());

推荐阅读