未从ActivateItem()激活ViewModel

时间:2019-07-25 14:13:08

标签: c# wpf caliburn.micro

启动应用程序时,默认情况下,我想打开一个用户控件,但只显示空的外壳视图。

遵循通常的Caliburn模式,并在ShellViewModel构造函数中使用SimpleContainer,一切正常,但看不到已激活的虚拟机。

ShellViewModel

public class ShellViewModel : Conductor<object>, IHandle<NewLayoutCreatedEvent>
    {
        private readonly SimpleContainer _container;
        private readonly IEventAggregator _events;
        private readonly IWindowManager _manager;

        public ShellViewModel(IEventAggregator events, IWindowManager manager, SimpleContainer container)
        {
            _events = events;
            _events.Subscribe(this);

            _manager = manager;
            _container = container;

            ActivateItem(_container.GetInstance<WorkLayoutViewModel>());
        }
}

WorkLayoutViewModel从Screen继承。在Bootstrapper类中,运行WorkLayoutViewModel的构造函数后会发生什么情况:

protected override object GetInstance(Type service, string key)
{
    return _container.GetInstance(service, key);
}

用空键调用一次,服务= ShellViewModel 然后继续:

protected override IEnumerable<object> GetAllInstances(Type service)
{
    return _container.GetAllInstances(service);
}

再次服务ShellViewModel,它在ShellView类中运行InitializeComponent()。 之后跳转到:

protected override void OnStartup(object sender, StartupEventArgs e)
{
    DisplayRootViewFor<ShellViewModel>();
}

发件人是应用程序本身,没有参数。在此之后,App类OnStartup()将覆盖我在其中设置语言并调用base.OnStartup()

的位置

我相信GetAllInstances也应该调用WorkLayoutViewModel而不只是ShellViewModel,这不会发生,并且视图也不会显示。

根据要求提供ShellView:

<Window x:Class="MyApp.Views.ShellView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MyApp.Views"
        xmlns:lang="clr-namespace:MyApp.Language;assembly=MyApp.Language"
        xmlns:interact="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:cal="http://www.caliburnproject.org"
        mc:Ignorable="d"
        Title="{x:Static lang:Resources.AppName}" Height="800" Width="1000" WindowStartupLocation="CenterScreen">
    <DockPanel>
        <StackPanel DockPanel.Dock="Top">
            <Menu FontSize="14" Height="27">
                <Menu.ItemsPanel>
                    <ItemsPanelTemplate>
                        <DockPanel HorizontalAlignment="Stretch" />
                    </ItemsPanelTemplate>
                </Menu.ItemsPanel>
                <MenuItem Header="{x:Static lang:Resources.Mnu_File}" HorizontalAlignment="Left">
                    <MenuItem Header="{x:Static lang:Resources.Mnu_New}">
                        <MenuItem.Icon>
                            <Image Source="/Images/new.png" />
                        </MenuItem.Icon>
                        <interact:Interaction.Triggers>
                            <interact:EventTrigger EventName="Click">
                                <cal:ActionMessage MethodName="NewLayout"/>
                            </interact:EventTrigger>
                        </interact:Interaction.Triggers>
                    </MenuItem>
                </MenuItem>
            </Menu>

            <ToolBar Height="36">
                <Button ToolTip="{x:Static lang:Resources.Tip_New}">
                    <Image Source="/Images/new.png" />
                    <interact:Interaction.Triggers>
                        <interact:EventTrigger EventName="Click">
                            <cal:ActionMessage MethodName="NewLayout"/>
                        </interact:EventTrigger>
                    </interact:Interaction.Triggers>
                </Button>
            </ToolBar>
        </StackPanel>
        <Grid>
            <ContentControl x:Name="ActivateItem" Margin="10 5 10 10" />
        </Grid>
    </DockPanel>
</Window>

WorkLayoutView只是带有红色背景的标准UserControl。

1 个答案:

答案 0 :(得分:1)

您在窗口内的ContentControl应该叫做ActiveItem而不是ActivateItemActiveateItem是设置ActiveItem属性的方法。