子视图模型上不会调用 OnInitialize 和 OnActivate

发布于 2024-12-11 21:15:29 字数 764 浏览 0 评论 0原文

我预计继承自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泪意 2024-12-18 21:15:29

看来这种行为不是默认情况下的,必须告诉父级使用 ConductWith 方法“传导”子视图模型,如下所示:

public class ParentViewModel : Screen
{
    public ChildViewModel Child { get; set; }

    public ParentViewModel(ChildViewModel childViewModel)
    {
        this.Child = childViewModel;

        Child.ConductWith(this);
    }
}

这确保了 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:

public class ParentViewModel : Screen
{
    public ChildViewModel Child { get; set; }

    public ParentViewModel(ChildViewModel childViewModel)
    {
        this.Child = childViewModel;

        Child.ConductWith(this);
    }
}

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.

沙沙粒小 2024-12-18 21:15:29

另一个选项是使父级成为 Conductor 类型并让孩子成为活动项目。

The other option is to make the parent a Conductor type and make the child the active item.

赢得她心 2024-12-18 21:15:29

其他解决方案是使用

protected override void OnViewAttached(object view, object context)

而不是 OnActivated()

Other solution is to use

protected override void OnViewAttached(object view, object context)

instead of OnActivated()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文