在Caliburn中停用后,多个屏幕中共享的屏幕消失。 [英] Screens shared in multiple screens disappear after deactivation in Caliburn.Micro

查看:114
本文介绍了在Caliburn中停用后,多个屏幕中共享的屏幕消失。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在多个父屏幕之间共享屏幕时,我使用Caliburn.Micro遇到了一个问题。在此示例中,我在外壳中有2个标签。每个选项卡共享一个 SharedViewModel 实例(基本上是一个单例)。

I've run across an issue using Caliburn.Micro when sharing a Screen between multiple parent Screens. In this example I have 2 tabs inside a shell. Each tab shares a single instance of SharedViewModel (essentially a singleton).

var shared = new SharedViewModel();

Items.AddRange(new []
{
    new TabViewModel { Shared = shared },
    new TabViewModel { Shared = shared },
});

每个选项卡的视图都有 ContentControl 绑定到 Shared 属性,即 SharedViewModel

And each tab's view has a ContentControl bound to the Shared property which is SharedViewModel:

<ContentControl cal:View.Model="{Binding Shared}"/>

以下是事件的顺序:


  1. 启动应用程序。查看共享视图。

  2. 切换到第二个标签。查看共享视图。

  3. 切换回第一个标签。 共享视图消失了。

  4. 再次切换到第二个标签。在那里共享视图。

  5. 继续拉出头发。

  1. Start the app. See the shared view.
  2. Switch to second tab. See the shared view.
  3. Switch back to first tab. Shared view is gone.
  4. Switch to second tab again. The shared view is there.
  5. Proceed to pull hair out.

您可以看到我的github 上的示例应用程序。

You can see the example application on my github.

推荐答案

Screen 类基于 ViewAware ,该类一次缓存并重用其视图它已加载。由于视觉对象的一个​​实例和同一实例不能多次附加到视觉树,因此会导致您观察到的行为。

The Screen class is based on ViewAware which caches and reuses its view once it has been loaded. As one and the same instance of a visual can't be attached to the visual tree multiple times, this leads to the behavior you're observing.

尝试在屏幕上覆盖它:

public override object GetView(object context = null)
{
    return null;
}

这篇关于在Caliburn中停用后,多个屏幕中共享的屏幕消失。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆