2013-10-14 93 views
0
设置Caliburn.Micro ControlContent

我是新来Caliburn.Micro (和MVVM为此事),我试图激活屏幕位于ShellViewModel从按钮子之内我的向导viewmodel(一个叫导体)。我见过的所有教程都有实际外壳中的按钮,可以在两者之间切换,所以我有点迷路。从另一个视图模型

所有的ViewModels共享命名空间SafetyTraining.ViewModels

ShellViewModel(第一次使用过一个壳,所以我可能在错误的方式来使用它)

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

    public void ShowMainView() 
    { 
     ActivateItem(new MainViewModel()); 
    } 
} 

ShellView XAML

<UserControl x:Class="SafetyTraining.Views.ShellView" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
<DockPanel> 
    <ContentControl x:Name="ActiveItem" /> 
</DockPanel> 

MainViewModel - 主屏幕(正确显示)。

public class MainViewModel : Screen 
{ 
    public void ShowLoginPrompt() 
    { 
     LoginPromptViewModel lg = new LoginPromptViewModel();//This does happen 
    } 
} 

MainView XAML

<Button cal:Message.Attach="[Event Click] = [ShowLoginPrompt]">Login</Button> 

LoginPromptViewModel

public class LoginPromptViewModel : Screen 
{ 
    protected override void OnActivate() 
    { 
     base.OnActivate(); 
     MessageBox.Show("Hi");//This is for testing - currently doesn't display 
    } 
} 

编辑工作代码:

修改嗅探器的代码位妥善适合我的结构。谢谢:)

var parentConductor = (Conductor<object>.Collection.OneActive)(this.Parent); 
     parentConductor.ActivateItem(new LoginPromptViewModel()); 

回答

1

你做正确的一切,但你缺少的一件事,但:

public void ShowLoginPrompt() 
{ 
    LoginPromptViewModel lg = new LoginPromptViewModel();//This does happen 
} 

你正在创建的LoginPromptViewModel的实例,但你说的不是导体,以激活该实例,所以它的OnActivate()方法永远不会被调用。

现在我才给你一个解决方案,我应该建议几件事情:如果您使用的是MainViewModel不同视图模型之间导航那么这将是适当的,使MainViewModel导体本身

  1. 如果您不是这样使用它,那么也许您应该将按钮导航到ShellView本身的LoginPromptViewModel

现在回到你的问题,因为你的MainViewModel扩展Screen然后它有一个Parent财产是指指挥,所以你可以做这样的:

public void ShowLoginPrompt() 
{ 
    LoginPromptViewModel lg = new LoginPromptViewModel();//This does happen 
    var parentConductor = (Conductor)(lg.Parent); 
    parentConductor.Activate(lg); 
} 
+0

它是定制的差异,或只是版本差异(我使用1.5.2),你通过Conductor no param(不适用于我)和Active而不是ActivateItem来做到这一点?只是想知道 – PRX

+0

@PRX对不起,我不完全明白你在问什么? –

+0

没关系我想我得到为什么XD – PRX

相关问题