【问题标题】:Switching between ViewModels in Caliburn Micro在 Caliburn Micro 中的 ViewModel 之间切换
【发布时间】:2019-12-11 23:44:13
【问题描述】:

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

【问题讨论】:

    标签: c# wpf mvvm caliburn.micro


    【解决方案1】:

    您首先需要从 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 的 Context Control 中,将其绑定到 Conductor 的 Active Item。

    <ContentControl x:Name="ActiveItem"/>
    

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

     ActivateItem(new UserControl2ViewModel());
    

    【讨论】:

      猜你喜欢
      • 2015-12-06
      • 2014-05-04
      • 1970-01-01
      • 1970-01-01
      • 2012-03-23
      • 1970-01-01
      • 2013-12-30
      • 1970-01-01
      • 2013-06-26
      相关资源
      最近更新 更多