当 Conductor Collection 设置为 AllActive 时如何停用项目

How to deactivate an item in when Conductor Collection is set to AllActive

我在停用已在 ContentControl 标记中定义的 ViewModel 时遇到问题。 我不想将 Visibility 设置为 false,因为这似乎是一个不合适的解决方案。

这就是它的样子,似乎在 AllActive 中,我们不需要激活每个项目:

<ContentControl Grid.Column="0" cal:View.Model="{Binding RandomScreenViewModel}"/>

这是 Conductor "OneActive" 模式下的样子。然后我们可以调用 ActivateItem 和 DeactivateItem,其中 Deactivate 将实际关闭 Content:

<ContentControl Grid.Column="0" x:Name="ActiveItem"}"/>

我希望激活的项目在 AllActive 期间关闭。这可能吗?在我看来,应该可以从 AllActive 中删除一个项目。

我对使用 Conductor 不太熟悉,所以我希望任何人都可以帮助我。 :D

ShellView.xaml:

Window x:Class="SimpleConductorOneActive.Views.ShellView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:cal="http://www.caliburnproject.org"
        xmlns:local="clr-namespace:SimpleConductorOneActive.ViewModels"
        Title="MainWindow" Height="350" Width="550" Background="CornflowerBlue">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal" Grid.Row="0">
            <Button Content="New Screen" x:Name="NewScreen" />
            <TextBlock Text="Current Screen Count:" VerticalAlignment="Center" Padding="10,5,2,5"/>
            <TextBlock Text="{Binding Items.Count}" VerticalAlignment="Center" Padding="0,5,5,5"/>
        </StackPanel>
        <ContentControl Grid.Row="1" cal:View.Model="{Binding RandomScreenViewModel}" HorizontalAlignment="Center"  VerticalAlignment="Center"  Height="200" Width="180" Margin="342,0,20,10" Padding="5"/>

        <ListBox x:Name='Items' Grid.Row="2">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical" Width="60" Margin="5">
                        <TextBlock Text="Screen Id" FontWeight="Bold"  />
                        <TextBlock Text='{Binding DisplayName}'  
                                   Margin='5 5 5 5'   
                                   Padding="5 5 5 5"
                                   FontSize='12' 
                                   TextWrapping="NoWrap"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation='Horizontal' />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
        </ListBox>

    </Grid>
</Window>

ShellViewModel.cs:

public class ShellViewModel : Conductor<object>.Collection.AllActive
{

    public RandomScreenViewModel RandomScreenViewModel { get; set; }

    public int ScreenCount
    {
        get { return Items.Count; }
    }

    public ShellViewModel()
    {
        RandomScreenViewModel = new RandomScreenViewModel();
        NewScreen();
    }

    public void NewScreen()
    {
        ActivateItem(RandomScreenViewModel);
    }
}

RandomScreenView.xaml:

<UserControl x:Class="SimpleConductorOneActive.Views.RandomScreenView"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid Background="AliceBlue">
        <StackPanel Orientation="Vertical">
            <Button Content="Close" HorizontalAlignment="Right" Margin="5" x:Name="CloseScreen" />
            <TextBlock Text="Name:"/>
            <TextBlock x:Name="DisplayName"/>

        </StackPanel>
    </Grid>
</UserControl>

RandomScreenViewModel.cs:

   public class RandomScreenViewModel:Screen
    {
        public RandomScreenViewModel()
        {
            Guid id = Guid.NewGuid();
            DisplayName = id.ToString();
        }

        public void CloseScreen()
        {
            base.TryClose();
        }

    }

在您的代码隐藏中,您可以将 ViewModel 的 IsEnabled 值设置为 false。我知道您说过您不想将可见性设置为 false,但您可以将可见性设置为 Collapsed。此解决方案会将其完全隐藏在您的输出中。

如果您想在代码隐藏中执行此操作,您需要执行以下操作:

yourObject.Visibility = Visibility.Collapsed;

我是 WPF、Caliburn Micro 的新手,我从未在实际应用程序中使用过 Conductor<T>.Collection.AllActive,但我已经尝试了一下。当您将它添加到 Items 即屏幕集合时,所有这些都被激活。
如果你想停用它,你可以简单地使用 DeactivateItem(T item, bool close) 方法来获取你正在执行的项目并且 bool 表示你也想关闭它。

这是代码片段。

ShellViewModel.cs

public void Remove()
{
    if (Items.Count > 0)
    {
        DeactivateItem(Items[0], true);
    }
}

ShellView.xaml

<ItemsControl x:Name="Items" />
<Button cal:Message.Attach="Remove"
        Width="50"
        Content="Deactive" />

当 Items 中的项目被删除时,ShellView 将得到更新,因为 Items 是实现 INotifyCollectionChangedIObservableCollection<T> 类型 我只尝试将项目与 ItemsControl 绑定,但另一个控件也应该能够筛选集合(我不知道 XD)