c# - ViewModel 未从 ActivateItem() 激活
问题描述
启动应用程序时,我想默认打开一个用户控件,但只显示空壳视图。
遵循通常的 Caliburn 模式并在 ShellViewModel 构造函数中使用 SimpleContainer 一切都执行得很好,但看不到激活的 vm。
外壳视图模型
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);
}
使用 null 键和 service = ShellViewModel 调用一次,然后继续:
protected override IEnumerable<object> GetAllInstances(Type service)
{
return _container.GetAllInstances(service);
}
where 再次服务 ShellViewModel,它在 ShellView 类中运行 InitializeComponent()。之后跳转到:
protected override void OnStartup(object sender, StartupEventArgs e)
{
DisplayRootViewFor<ShellViewModel>();
}
发件人是应用程序本身,没有参数。在此之后,在我设置语言并调用 base.OnStartup() 的地方调用了 App 类 OnStartup() 覆盖
I believe the GetAllInstances should call the WorkLayoutViewModel too not just the ShellViewModel, this is not happening and the view is not displayed.
As requested the 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>
and the WorkLayoutView is just a standard UserControl with red back ground.
解决方案
Your ContentControl
inside the window should probably be called ActiveItem
not ActivateItem
. ActiveateItem
is the method that sets the ActiveItem
property.
推荐阅读
- selenium - 当测试 Maven 任务之一失败时,Azure 管道构建失败
- macos - 无法在 MacBook 上的浏览器中访问 Kubernetes Ingress
- macos - OpenAI Gym:PGDrive 中的 env.reset() 失败
- python - 如何修复 create() 需要 1 个位置参数,但在 python django 中给出了 5 个
- php - 嵌套 Eloquent 关系的循环和改变值
- javascript - 我无法让科尔多瓦计步器插件工作
- asp.net-core-webapi - 在特定环境下下载文件时获取 ERR_HTTP2_PROTOCOL_ERROR
- prometheus - Prometheus 小于最大值表达式
- docker - Kafka 代理无法在 Docker 中启动
- eleventy - “上次修改时间”不适用于自定义变量