除非被导体激活,否则Caliburn.Micro IChild.Parent为null [英] Caliburn.Micro IChild.Parent is null unless activated by conductor
问题描述
我有一个父级和子级视图模型:
I have a Parent and Child viewmodel:
public class ParentViewModel : Screen
{
public ChildViewModel Child { get; set; }
}
public class ChildViewModel : PropertyChangedBase, IChild { }
使用 Conductor.ActivateItem()
显示父VM时,Caliburn.Micro会执行通常的工作,即搜索视图模型对象图并查找要显示的视图等。
When the Parent VM is displayed using Conductor.ActivateItem()
, Caliburn.Micro does the usual labours of searching through the view model object graph and looking for views to display etc.
所有这些完成之后,我发现虽然 ParentViewModel.Parent
是对导体的引用, ChildViewModel.Parent
为 null
。
After all this is completed, I find that while ParentViewModel.Parent
is a reference to the Conductor, ChildViewModel.Parent
is null
.
这是设计使然吗?
非常感谢。
推荐答案
是的,这是通过设计中,设置 Parent
属性的方法是 ConductorBase $ c中的
EnsureItem
$ c>,因此只有在更改活动项目时才会设置导体。
Yes this is by design, the method that sets the Parent
property is EnsureItem
in the ConductorBase
, therefore it's only conductors that will set it when the active item is changed.
父母
也仅在孩子的激活生命周期中可用,例如 OnInitialise
或 OnActivate
等。
The Parent
is therefore also only available in the activation life cycle of the child, e.g. OnInitialise
or OnActivate
etc.
有什么原因吗?您的 ParentViewModel
不是 Conductor
类型,而您的 ChildViewModel
屏幕?
Is there any reason your ParentViewModel
isn't a Conductor
type and your ChildViewModel
a Screen?
此外,取决于父项
属性,可能会在您的中引入耦合ChildViewModel
。
Also, depending on the Parent
property may be introducing coupling in your ChildViewModel
.
这篇关于除非被导体激活,否则Caliburn.Micro IChild.Parent为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!