子视图模型上不会调用 OnInitialize 和 OnActivate
我预计继承自 Screen 的子视图模型将参与父 Screen 的生命周期。然而,情况似乎并非如此。例如:
public class ParentViewModel : Screen
{
public ChildViewModel Child { get; set; }
public ParentViewModel(ChildViewModel childViewModel)
{
this.Child = childViewModel;
}
public override void OnInitialize() { // called - as expected }
public override void OnActivate() { // called - as expected }
public override void OnDeactivate() { // called - as expected }
}
public class ChildViewModel : Screen
{
public override void OnInitialize() { // not called - why? }
public override void OnActivate() { // not called - why? }
public override void OnDeactivate() { // not called - why? }
}
是否可以让子 Screen 参与父 Screen 的生命周期?
I expected that child View Models inheriting from Screen would participate in the Parent Screen's life-cycle. However, it appears this is not the case. For example:
public class ParentViewModel : Screen
{
public ChildViewModel Child { get; set; }
public ParentViewModel(ChildViewModel childViewModel)
{
this.Child = childViewModel;
}
public override void OnInitialize() { // called - as expected }
public override void OnActivate() { // called - as expected }
public override void OnDeactivate() { // called - as expected }
}
public class ChildViewModel : Screen
{
public override void OnInitialize() { // not called - why? }
public override void OnActivate() { // not called - why? }
public override void OnDeactivate() { // not called - why? }
}
Is it possible to have a child Screen participate in the parent Screen's life-cycle?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看来这种行为不是默认情况下的,必须告诉父级使用
ConductWith
方法“传导”子视图模型,如下所示:这确保了 ChildViewModel 将在与父母同一时间。如果您只需要初始化/激活子级,则可以使用
ActivateWith
方法。It seems this behaviour is not by default and the parent has to be told to 'conduct' child View Models using the
ConductWith
method, as follows:This ensures the ChildViewModel will be initialized, activated and deactivated at the same time as the parent. The
ActivateWith
method can be used if you only need to initialize/activate the child.另一个选项是使父级成为 Conductor 类型并让孩子成为活动项目。
The other option is to make the parent a Conductor type and make the child the active item.
其他解决方案是使用
而不是 OnActivated()
Other solution is to use
instead of OnActivated()