2013-12-08 61 views
0

我使用的樣品從http://www.mindscapehq.com/blog/index.php/2013/09/11/caliburn-micro-part-6-introduction-to-screens-and-conductors/從一個屏幕導航到另一個在卡利

有一個AppViewModel其中其它的ViewModels通過調用ActivateItem激活。

示例正在爲我工​​作:我可以看到相應的視圖。

我現在想要從另一個ViewModel激活一個ViewModel。它被實例化,但不顯示相應的視圖。

如何從「RedScreenViewModel」激活「GreenScreenViewModel」?

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

如果你想在你AppViewContentControl顯示ActiveItem當你按下更改ButtonRedViewModel,你將需要使用EventAggregator從您RedViewModel傳遞消息給你的AppViewModel

Mindscape EventAggregator tutorial

+0

OMG我什至經歷過那個教程。這一定會奏效。 –

+0

@MalcolmFrexner - 哈哈,這些東西很容易在你工作了一段時間或被「分區」時錯過。 – 2013-12-09 08:34:58

+0

@Malcolm Frexner您是否最終通過事件聚合器使其工作? – liang

相關問題