1

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.

Kris McGinnes
  • 372
  • 2
  • 7

1 Answers1

2

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;
}
Frank
  • 4,461
  • 13
  • 28
  • This worked like a charm, with an addendum. The `Screen` base class does not mark `GetView` as `virtual`, so you must mark the method as `new`. Thanks! – Kris McGinnes Dec 10 '14 at 17:29
  • @KrisMcGinnes Actually, GetView should be defined in ViewAware as virtual, at least since version 2. Please look into https://github.com/Caliburn-Micro/Caliburn.Micro/blob/master/src/Caliburn.Micro/ViewAware.cs – Frank Dec 10 '14 at 21:10
  • Good catch. I think I was using 2.0 and not 2.0.1 which has `GetView` as virtual. – Kris McGinnes Dec 11 '14 at 20:27