问题
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:
- 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.
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