Activating Screen vs POCO

Topics: UI Architecture
Apr 5, 2012 at 7:06 PM

The following sample works correctly, however changing FirstViewModel to the following causes FirstView to only be shown the first time it is navigated to.

 

public class FirstViewModel : Screen { }

Upon subsequent navigation nothing appears within the First View Tab. Could someone please help me understand why that happens?

Viewmodel code

public enum ShellMode
{
    Detail = 1,
    Edit = 2
}

public class ShellViewModel : Conductor<object>.Collection.OneActive
{
    public ShellViewModel()
    {
        Detail();
    }

    public void Detail()
    {
        var firstVM = new FirstViewModel();
        ActivateItem(firstVM);
        Mode = ShellMode.Detail;
    }

    public void Edit()
    {
        var secondVM = new SecondViewModel();
        ActivateItem(secondVM);
        Mode = ShellMode.Edit;
    }

    protected ShellMode mode;
    public ShellMode Mode
    {
        get { return mode; }
        set
        {
            if (mode != value)
            {
                mode = value;
                NotifyOfPropertyChange(() => Mode);
            }
        }
    }
}

public class FirstViewModel { }

public class SecondViewModel { }

ShellView.xaml

<Window x:Class="SmallActivateTest.ShellView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro"
        Title="ShellView" Height="300" Width="300">
    <StackPanel>
        <TextBlock Text="Shell View" />
        <UserControl 
            cal:View.Context="{Binding Mode, Mode=TwoWay}"
            cal:View.Model="{Binding}" />        
    </StackPanel>
</Window>
Shell\Detail.xaml

<UserControl x:Class="SmallActivateTest.Shell.Detail"
             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">
    <DockPanel>
        <TabControl>
            <TabItem Header="Detail data">
                <TextBlock Text="Detail View" />
            </TabItem>
            <TabItem Header="First View Tab">
                <ContentControl x:Name="ActiveItem" />
            </TabItem>
        </TabControl>
        <Button x:Name="Edit" Content="Edit" Width="100" />
    </DockPanel>
</UserControl>

Shell\Edit.xaml

<UserControl x:Class="SmallActivateTest.Shell.Edit"
             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">
    <DockPanel>
        <TabControl>
            <TabItem Header="Edit data">
                <TextBlock Text="Edit View" />
            </TabItem>
            <TabItem Header="Second View Tab">
                <ContentControl x:Name="ActiveItem" />
            </TabItem>
        </TabControl>
        <Button x:Name="Detail" Content="Detail" Width="100" />
    </DockPanel>
</UserControl>

 

Apr 6, 2012 at 4:10 PM

What are you trying to do?  I'm not sure why it's doing what it is but possible has something to do with not being able to properly activate or deactivate the poco because it doesn't implement IActivate or IDeactivate.  

Apr 6, 2012 at 6:02 PM

Hi,

Thank you for your reply.

The poco works fine, it's when the FirstViewModel is a screen that the corresponding view is not shown.  Does that mean it isn't being activated for some reason?

The scenario is similar to the Caliburn.Micro.SimpleNavigation sample with multiple views over the same viewmodel and a tab control thrown into the mix.