c# - 在 Caliburn Micro 中的 ViewModel 之间切换
问题描述
我正在尝试使用 Caliburn Micro 制作 WPF 应用程序。我在整页上有一个带有 ContentControl 的 ShellView。我已经在 ShellView 的 ContentControl 启动时显示了一个 UserControl(基本上它是一个登录页面)。登录后,我想关闭当前的 ViewModel 并在 ShellView 的 ContentControl 中显示另一个。我怎样才能做到这一点?
解决方案
您需要首先从 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());
推荐阅读
- javascript - 如何使用 JavaScript 检测日期是否过去?
- android - 通过 BLE 的音乐播放器
- excel - Excel VBA 代码优化,使用公式和代码添加新行
- node.js - 删除 'node_modules' 文件夹,但它会在一段时间后重新生成
- c# - 如何从 C# 中的 Json 数组中获取数据?
- node.js - 无法使用 async.each() 异步迭代具有异步块的数组
- python - while循环内的错误
- python - AWS Lightsail Cloud9 安装失败
- javascript - 在响应式画布上绘图时光标和绘图之间的间隙
- java - spring cloud 快速入门演示 maven