Problem with regions

Topics: Prism v1
Dec 29, 2009 at 9:24 AM
Edited Dec 29, 2009 at 9:24 AM


I have a shell with 2 regions: "MenuRegion", "MainRegion".
I have for example 1 module named CoolModule:

        public CoolModule(IUnityContainer container)
            _container = container;
            _regionManager = _container.Resolve<IRegionManager>();
            _manager = _container.Resolve<IManager>();
            OpenView = new DelegateCommand<object>(ExecOpenView);

            Menu = new MenuControl {DataContext = this};

        public MenuControl Menu { get; private set; }
I'd like, that whole program manager (IManager) will be able to add different views to MainRegion (TabControl).
And, when I click to button on menu, ExecOpenView method will be executed.
        private void ExecOpenView(object obj)
            var container = _container.Resolve<IContainerViewModel>();
In this method I create new ViewModel with view and tell to IManager to place this in "MainRegion" region as a new TabPage.
All works fine, until, IContainerView contains another View, that contains own region. The first view is created successfully, and the next throw Exception:
"Specified element is already the logical child of another element. Disconnect it first."
ContainerViewModel looks like:
        public ContainerViewModel(IUnityContainer container)
            _container = container;
            _regionManager = _container.Resolve<IRegionManager>();

            var vm = _container.Resolve<IDetailsViewModel>();
            _regionManager.RegisterViewWithRegion("Details", () => vm.View);

            Title = Guid.NewGuid().ToString();

            View = _container.Resolve<IContainerView>();
            View.Model = this;

What I need to do to resolve my problem? I tried to change:
            _regionManager = _container.Resolve<IRegionManager>().CreateRegionManager();
but nothing helps.
The whole project is here:
Thanks in advance and sorry for my English :)



Dec 30, 2009 at 10:44 PM

Not 100% sure but I suspect for this you are running into lifetime issues. Are the two instances of views supposed to be the same or in fact 2 separate instances? If they are separate, then it could be that you need to change the lifetime manager for the view to TransientLifetimeManager so it will create a new instance rather than getting the existing single instance.

Jan 4, 2010 at 1:59 PM

Two instances of views are really two different instances. I've checked by GetHashCode :)