在Caliburn中从一个屏幕导航到另一个屏幕

时间:2013-12-08 21:07:38

标签: wpf mvvm caliburn.micro

我正在使用http://www.mindscapehq.com/blog/index.php/2013/09/11/caliburn-micro-part-6-introduction-to-screens-and-conductors/

中的示例

有一个AppViewModel,其中通过调用ActivateItem激活其他ViewModel。

示例正在为我工​​作:我可以看到相应的视图。

我现在想要从另一个ViewModel激活ViewModel。它被实例化,但不显示相应的视图。

如何激活" GreenScreenViewModel"来自" RedScreenViewModel"?

APPVIEW:

    <UserControl x:Class="CaliburnMicroApp_Navigation.AppView"
             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">
  <DockPanel  Background="LightBlue" MinHeight="400" MinWidth="400">
    <StackPanel Orientation="Horizontal" DockPanel.Dock="Top" HorizontalAlignment="Center">
      <Button x:Name="ShowRedScreen" Content="Red" Width="50" />
      <Button x:Name="ShowGreenScreen" Content="Green" Width="50" Margin="12,0,0,0" />
      <Button x:Name="ShowBlueScreen" Content="Blue" Width="50" Margin="12,0,0,0" />
    </StackPanel>
    <ContentControl x:Name="ActiveItem" />
  </DockPanel>
</UserControl>

AppViewModel

public class AppViewModel : Conductor<object>
{
  public void ShowRedScreen()
  {
    ActivateItem(new RedViewModel());
  }

  public void ShowGreenScreen()
  {
    ActivateItem(new GreenViewModel());
  }

  public void ShowBlueScreen()
  {
    ActivateItem(new BlueViewModel());
  }
}

RedViewModel - 这不显示GreenView()

public class RedViewModel :   Conductor<object>
  {
      public void DisplayGreen()
      {
          ActivateItem(new GrenViewModel());
      }
  }

RedView

 <Grid Background="Red">
      <StackPanel>
    <TextBlock Text="red" FontSize="48" FontWeight="Bold" Foreground="#3CA527"  />
      <Button Name="DisplayGreen">
            <TextBlock >Next Screen</TextBlock>
        </Button>
        </StackPanel>
    </Grid>

1 个答案:

答案 0 :(得分:1)

如果您希望在ActiveItem中按ContentControlAppView Button中显示的RedViewModel更改,则需要使用EventAggregatorRedViewModel的消息传递给您的AppViewModel

Mindscape EventAggregator tutorial