简体   繁体   English

ActivateItem在Caliburn.Micro中不起作用

[英]ActivateItem not working in Caliburn.Micro

I am using Caliburn Micro for MVVM. 我正在将Caliburn Micro用于MVVM。 In my MainView (shell), I have two controls. 在我的MainView(外壳)中,我有两个控件。 One hosts a RibbonView and another ContentControl which loads contents depending on the RibbonView menu selection. 一个承载一个RibbonView,另一个承载ContentControl,该控件根据RibbonView菜单选择加载内容。 Here is the MainView (shell) 这是MainView(外壳)

MainView (shell) MainView(外壳)

<Window x:Class="HotelReservation.Main.Views.MainView">
    <DockPanel>

        <ContentControl x:Name="RibbonView" DockPanel.Dock="Top"/>

        <Grid DockPanel.Dock="Bottom" VerticalAlignment="Stretch" >
            <ContentControl x:Name="ActiveItem"/>
        </Grid>
    </DockPanel>
</Window>

RibbonView RibbonView

<Ribbon Margin="0,-20,0,0">
    <RibbonTab Header="Room Band">
        <RibbonGroup>
            <RibbonButton Label="List" x:Name="RoomBandMain"
                             LargeImageSource="/HotelReservation.Global;component/Images/room-band-list-icon.png">
            </RibbonButton>
        </RibbonGroup>
    </RibbonTab>
</Ribbon>

RibbonViewModel RibbonViewModel

public class RibbonViewModel : Conductor<object> {

    public void RoomBandMain() { //Load in ActiveItem of MainView
        ActivateItem(container.GetExportedValue<RoomBandMainViewModel>());
    }
}

As can be seen, I am trying to load RoomBandMainViewModel in the <ContentControl x:Name="ActiveItem"/> The issue is that it is not loaded and I get a blank screen even though ActivateItem(container.GetExportedValue<RoomBandMainViewModel>()) code runs. 可以看出,我正在尝试在<ContentControl x:Name="ActiveItem"/>加载RoomBandMainViewModel ,问题是它没有加载,即使ActivateItem(container.GetExportedValue<RoomBandMainViewModel>())代码运行。 I think that the <ContentControl x:Name="ActiveItem"/> exists not in RibbonView but its parent MainView , and hence the ActivateItem doesn't work. 我认为<ContentControl x:Name="ActiveItem"/>不存在于RibbonView中,而是存在于其父MainView中 ,因此ActivateItem不起作用。

How to resolve this issue. 如何解决这个问题。

Edit: 编辑:

I had to set the DataContext of the <ContentControl x:Name="ActiveItem"/> to RibbonViewModel, so that ActiveItem is now property of RibbonViewModel and not MainViewModel. 我必须将<ContentControl x:Name="ActiveItem"/>的DataContext设置为RibbonViewModel,以便ActiveItem现在是RibbonViewModel的属性,而不是MainViewModel。 MainViewModel looks like below MainViewModel如下所示

So the MainView (shell) is now as follows 所以MainView(shell)现在如下

<Window x:Class="Conductor_Main.Views.MainView">

    <DockPanel>
        <ContentControl x:Name="RibbonView" DockPanel.Dock="Top"/>

        <Grid DockPanel.Dock="Bottom" VerticalAlignment="Stretch" Background="Green"  
               DataContext="{Binding RibbonView}">

            <ContentControl x:Name="ActiveItem" />
        </Grid>
    </DockPanel>
</Window>

Now the <ContentControl x:Name="ActiveItem" /> actually belongs to the RibbonViewModel. 现在, <ContentControl x:Name="ActiveItem" />实际上属于RibbonViewModel。

What you have here is some kind of lifecycle of your windows. 您这里拥有的是Windows的某种生命周期。 This has to be handled by the parent window of your ActiveItem . 这必须由ActiveItem的父窗口来处理。

The way to get this done the caliburn.micro way is to have a Conductor above the ActiveItem . caliburn.micro方法的实现方法是在ActiveItem上方ActiveItem一个Conductor。 In your case this is the MainWindow . 在您的情况下,这是MainWindow

Your RibbonViewModel can be a Conductor, too. 您的RibbonViewModel也可以是导体。 But only for it's own children. 但这只是为了自己的孩子。 There can be more than one conductor. 可以有多个导体。

From the caliburn documentation caliburn文档

Once you introduce the notion of a Screen Activation Lifecycle into your application, you need some way to enforce it. 将屏幕激活生命周期的概念引入应用程序后,您需要某种方式来实施它。 This is the role of the ScreenConductor. 这就是ScreenConductor的角色。 When you show a screen, the conductor makes sure it is properly activated. 显示屏幕时,导体将确保其已正确激活。

Which is quite a direct way of saying: If you have activation / life cycle, then use a conductor. 这是一个很直接的说法:如果您具有激活/生命周期,请使用导体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM