【问题标题】:Caliburn Micro Conductor.Collection.AllActive not workingCaliburn Micro Conductor.Collection.AllActive 不工作
【发布时间】:2018-06-28 07:16:45
【问题描述】:

我尝试使用带有 Conductor.Collection.AllActive 的 Caliburn Micro 在应用程序中激活多个窗口

遵循的步骤:

从 Conductor.Collection.AllActive 继承的 MainHomeViewodel

1)创建的属性

public ExploreViewModel Explorer {
   get; private set;  
 }

2) 以名称作为属性名称创建 ContentControl

<ContentControl x:Name="Explorer" />

3) 具有属性的激活视图模型

Explorer = new ExplorerViewModel();
ActivateItem(Explorer );

执行上述代码后,它会实例化 ExplorerViewModel 但不会转到 View 的构造函数或显示 View 。

上述实现有任何问题,或者我需要做更多的事情来激活项目。

请帮忙!

谢谢。

编辑

    public class MainHomeWindowViewModel : Conductor<IScreen>.Collection.AllActive
    {
      protected override void OnInitialize()
      {
       base.OnInitialize();
       ShowExplorer();
       }
        public void ShowExplorer()
        {

            Explorer = new ExplorerViewModel();
            ActivateItem(Explorer );

        }
}

【问题讨论】:

  • 你能展示全班吗?你在哪里打电话ActivateItem(Explorer)
  • @FCin 已编辑问题,请检查。它调用 ExplorerViewModel 但不显示视图。

标签: c# wpf xaml uwp caliburn.micro


【解决方案1】:

Conductor.Collection.AllActive 使用 Items 属性。如果要一次显示多个屏幕,则必须将它们添加到Items 属性中。

然后,由于您的视图存储在Items 属性中,您希望将视图绑定到Items。这是一个例子:

指挥:

public class ShellViewModel : Conductor<IScreen>.Collection.AllActive
{
    public ShellViewModel()
    {
        Items.Add(new ChildViewModel());
        Items.Add(new ChildViewModel());
        Items.Add(new ChildViewModel());
    }
}

导体视图(注意,因为我们显示了我们想要使用的项目集合ItemsSource 而不是ContentControl):

<Grid>
    <StackPanel>
        <ItemsControl x:Name="Items"></ItemsControl>
    </StackPanel>
</Grid>

子屏幕:

public class ChildViewModel : Screen
{
}

子视图:

<Grid>
    <Border Width="50" Height="50" BorderBrush="Red" BorderThickness="5"></Border>
</Grid>

编辑:关于 cmets 中的讨论,这里是如何使用 IWindowManager 显示多个窗口:

public class ShellViewModel : Screen
{

    public ShellViewModel(IWindowManager windowManager)
    {
        var window1 = new ChildViewModel();
        var window2 = new ChildViewModel();

        windowManager.ShowWindow(window1);
        windowManager.ShowWindow(window2);

        window1.TryClose();
    }
}

【讨论】:

  • 谢谢,让我试试这个方法。将视图模型添加到不需要调用 ActivateItem 方法的 Items 后,还有一个疑问?
  • @VyasDev 对于Conductor.Collection.AllActiveActivateItem() 在内部执行与Items.Add 相同的操作。你可以使用任何你真正想要的。
  • 现在 ChildViewModel 的视图是 User Control ,但我的要求是添加 Windows 而不是 UserControls 。那么我们可以使用 ContentControl 而不是 ItemsControl 来容纳多个窗口或者我们如何实现它?
  • @VyasDev 所以你不想使用导体。您想使用IWindowManager 并使用ShowWindow() 方法。
  • @VyasDev 更新了我的答案。
猜你喜欢
  • 2015-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-29
  • 2012-10-24
  • 2012-05-08
  • 1970-01-01
相关资源
最近更新 更多