我正在尝试使用 Caliburn Micro 制作 WPF 应用程序。我在整页上有一个带有 ContentControl 的 ShellView。我已经在 ShellView 的 ContentControl 中显示了一个 UserControl(基本上它是一个登录页面)。登录后,我想关闭当前的 ViewModel 并在 ShellView 的 ContentControl 中显示另一个。我怎样才能做到这一点?
最佳答案
您需要首先从 Conductor 类继承 ShellViewModel,从 Screen 继承其他 ViewModel(Login 和 SecondViewModel)。您可以在 Screen and Conductors 上阅读更多信息.例如,
public class ShellViewModel:Conductor<Screen>
public class UserControl1ViewModel: Screen
public class UserControl2ViewModel: Screen
ShellViewModel 将在不同的屏幕之间进行,并且继承自 Caliburn.Micro 的 Conductor 类。当您显示屏幕时,指挥会确保它已正确激活。如果您正在从屏幕过渡,它会确保它被停用。
您需要进行的第二个更改是在 ShellView 中的上下文控件中,方法是将其绑定(bind)到 Conductor 的事件项。
<ContentControl x:Name="ActiveItem"/>
最后,您可以使用 Conductor 的 ActivateItem 方法在屏幕之间切换。
ActivateItem(new UserControl2ViewModel());
关于c# - 在 Caliburn Micro 中的 ViewModel 之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57341852/