new ContainerControlledLifetimeManager() raised Exception

Topics: Prism v2 - WPF 3.5
Feb 11, 2010 at 2:01 AM

Hi @all,


I try to implement a ContainerControlledLifetimeManager like in this example. But when I resolve the ViewModel, an exception will be raised:

Specified element is already the logical child of another element. Disconnect it first.


private void RegisterViewsAndServices()
            this.container.RegisterType<IHomeController, HomeController>();
            this.container.RegisterType<IHomeViewModel, HomeViewModel>(new ContainerControlledLifetimeManager());
            this.container.RegisterType<IHomeView, HomeView>(new ContainerControlledLifetimeManager());

        public void ShowView()
            () => this.container.Resolve<IHomeViewModel>().View);



            <ColumnDefinition Width="{Binding MainMenuWidth}"/>
            <ColumnDefinition />

        <Border Grid.Column="0">
            <ContentControl regions:RegionManager.RegionName="{x:Static inf:ShellRegionNames.MainMenu}"/>

        <Border Background="{StaticResource BackgroundBrush}" Grid.Column="1">
            <ContentControl regions:RegionManager.RegionName="{x:Static inf:ShellRegionNames.Main}"/>



Thanks for your help,


Feb 11, 2010 at 6:23 PM

Hi Manullino,

The ContainerControlledLifeTimeManager is used to register mappings with Singleton lifetime. Therefore, a possible reason for this to happen is if you already added the view of the HomeViewModel to any other region. As the same instance of the ViewModel (and the View) is returned when they are resolved, you can only add them to a single region at a time.

If the above does not reflect your scenario, please provide a small sample application so we can look deeper into this issue.

Please let me know if this helps.

Damian Schenkelman


Feb 24, 2010 at 12:22 AM

I tried to resolve the Shell. This causes the problem.

How can I fix this? Or doesn't it make sense to resolve the Shell?


public class Bootstrapper : UnityBootstrapper
        protected override DependencyObject CreateShell()
            IShellView shell = Container.Resolve<IShellView>();
            IShellViewModel shellViewModel = Container.Resolve<IShellViewModel>();
            shell.ViewModel = shellViewModel;

            return shell as DependencyObject;