Screens shared in multiple screens disappear after deactivation in Caliburn.Micro

别说谁变了你拦得住时间么 提交于 2019-12-24 15:13:06

问题


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 },
});

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

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

Here's the order of events:

  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.

You can see the example application on my github.


回答1:


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.

Try overriding this in your screen:

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


来源:https://stackoverflow.com/questions/27390932/screens-shared-in-multiple-screens-disappear-after-deactivation-in-caliburn-micr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!