C# Caliburn Micro 管理 windows/views

C# Caliburn Micro managing windows/views

我目前正在用 C# 开发 WPF 应用程序。我使用 Caliburn Micro 作为框架。 MainWindow (ShellView) 主要由左右两部分组成。这两个部分都是 TabControl。左侧有 3 个选项卡,右侧有 6 个选项卡。

目前我正在使用 ShellViewModel 的构造函数实例化 9 个视图的 9 个 ViewModel,然后将带有 ContentControl 的 View 绑定到 TabControl。

通常使用 Caliburn Micro,使用 Conductor-class 可以更好地处理。我可以创建一个列表并将该列表绑定到 TabControl,仅此而已。

但问题是我只有一次 Items-Collection,因为它是从 Conductor-class 继承的。因此,如果我将左侧 TabControl 绑定到与右侧 TabControl 相同的项目,我将在两侧拥有相同的选项卡,这不是我想要的。

有没有办法拥有 2 个独立的项目集合?

谢谢

您需要为主屏幕的每一侧创建一个单独的视图模型。这些视图模型中的每一个都可以继承自 Conductor<object>.Collection.OneActive class。然后,您的 shell 视图模型将由两个子视图模型组成。您的 shell 视图也将由两个内容控件组成,其中将显示每个子视图模型和子视图。

public class ShellViewModel : Conductor<object>.Collection.AllActive {
   public LeftSideViewModel LeftSide { get; set; }
   public RightSideViewModel RightSide { get; set; }

   public ShellViewModel(LeftSideViewModel leftSide, RightSideViewModel rightSide) {
      LeftSide = leftSide;
      RightSide = rightSide;
      ActivateItem(leftSide);
      ActivateItem(rightSide);
   }
}

<UserControl x:Class="Workbench.Views.ShellView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <DockPanel LastChildFill="True">
        <ContentControl x:Name="LeftSide" DockPanel.Dock="Top"/>

        <ContentControl x:Name="RightSide"/>
    </DockPanel>
</UserControl>

public class LeftSideViewModel : Conductor<object>.Collection.OneActive {
   public LeftSideViewModel(...) {
       ActivateItem(yourTabViewModel);
       ActivateItem(anotherTabViewModel);
       ...
   }
}

public class RightSideViewModel : Conductor<object>.Collection.OneActive {
   public RightSideViewModel(...) {
       ActivateItem(yourTabViewModel);
       ActivateItem(anotherTabViewModel);
       ...
   }
}

<UserControl x:Class="Workbench.Views.LeftSideView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:cal="http://www.caliburnproject.org"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">

    <Grid>
        <TabControl x:Name="Items" BorderThickness="0" TabStripPlacement="Bottom">
            <TabControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding TabText}" />
                        <!-- The grid will not appear (collapsed) if the tab cannot be closed -->
                        <Grid Visibility="{Binding CloseTabIsVisible, Converter={StaticResource BoolToVisibilityConverter}}">
                            <!-- The tab close button -->
                            <Button x:Name="CloseTab" Cursor="Hand" Focusable="False" Content="X" cal:Message.Attach="[Click] = [CloseTab($this)]" />
                        </Grid>
                    </StackPanel>
                </DataTemplate>
            </TabControl.ItemTemplate>
        </TabControl>
    </Grid>
</UserControl>