在Caliburn中停用后,多个屏幕中共享的屏幕消失。 [英] Screens shared in multiple screens disappear after deactivation in Caliburn.Micro
问题描述
在多个父屏幕之间共享屏幕
时,我使用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}"/>
以下是事件的顺序:
- 启动应用程序。查看共享视图。
- 切换到第二个标签。查看共享视图。
- 切换回第一个标签。 共享视图消失了。
- 再次切换到第二个标签。在那里共享视图。
- 继续拉出头发。
- Start the app. See the shared view.
- Switch to second tab. See the shared view.
- Switch back to first tab. Shared view is gone.
- Switch to second tab again. The shared view is there.
- 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屋!