【问题标题】:Calling another ViewModel using Caliburn.micro使用 Caliburn.micro 调用另一个 ViewModel
【发布时间】:2013-05-03 19:49:15
【问题描述】:

我有两个视图,分别称为 A 和 B。当用户对 A 执行特定操作时,我希望它启动 B。两个视图都应保持打开状态。

A 继承了 Conductor,我尝试调用 ActivateItem,但视图 B 从未打开。

我错过了什么?这是使用 Caliburn.micro 从另一个视图调用一个视图的正确方法吗?

【问题讨论】:

  • 您想在另一个窗口中启动 B 视图吗?或者你打算在一个窗口中有多个屏幕?
  • 我想在不同的窗口中启动它。 A 和 B 都应该显示在自己的窗口中。

标签: c# wpf caliburn.micro


【解决方案1】:

好的,如果 B 视图应该在不同的窗口中,这是最简单的解决方案。我假设 AViewModel 是“外壳”视图模型:

public class AViewModel // : Screen - that's optional right here ;)
{
    private readonly BViewModel bViewModel;
    private readonly IWindowManager windowManager;

    public AViewModel(BViewModel bViewModel, IWindowManager windowManager)
    {
        this.bViewModel = bViewModel;
        this.windowManager = windowManager;
    }

    public void ShowB()
    {
        windowManager.ShowWindow(bViewModel); //If you want a modal dialog, then use ShowDialog that returns a bool?
    }
}

和最简单的视图(AView):

<Button Name="ShowB" Content="Show another window"/>

当您单击AView 上的按钮时,它将显示带有BView 的新窗口。

导体用于在一个窗口中有多个屏幕(或者,换句话说,一个屏幕上有多个窗格)。有时是 OneActive(例如,在 WPF 中实现导航时)或 AllActive,当同时有多个屏幕可用时。

Here's 一个很好的教程,介绍了 Caliburn.Micro 的一些基础知识,并且该特定部分描述了窗口管理器。

【讨论】:

  • 谢谢你给了我我需要的东西。该教程写得很好,应该也会有所帮助。
猜你喜欢
  • 2022-10-18
  • 2013-10-22
  • 1970-01-01
  • 1970-01-01
  • 2017-12-06
  • 2013-11-12
  • 1970-01-01
  • 1970-01-01
  • 2011-05-01
相关资源
最近更新 更多