views that have been removed show up again when dialog is reopned

Topics: Prism v4 - WPF 4
Dec 3, 2010 at 8:50 PM

Hi there, 

Let me try and explain, I have a window and in this window is a view, it is a wizard so it has a bunch of radio buttons in a listbox and when the user clicks an option it moves them to the next view, when I do this I call this little bit of code. 

        private void ClearRegions()
        {
          IRegion region = this._container.Resolve<IRegionManager>().Regions["ShellDialogBodyRegion"];
          var views = region.Views;
          foreach (var view in views)
          {
            region.Remove(view);
          }
        }

That works fine. If they start over I need to remove the whatever view they are on and show the main view again, the same view that is shown when the module is initialized. I do that like this. 

        private void OnBackCommand()
        {
          IRegion region = RegionManager.Regions["ShellDialogBodyRegion"];
          var views = region.Views;
          
          foreach (var view in views)
          {
            region.Remove(view);
          }
          RegionManager.RegisterViewWithRegion("ShellDialogBodyRegion", () => this.Container.Resolve<Views.CreateRgaView>());
        }

Everything is working fine, I can go backward and move through screens and then start over 100 times and it all works fine except if I close and reopen the window (just the window mind you the app itself is still running)... When I do that I see the main view as many times as I hit the back button, say OnBackCommand was called three times, I would see CreateRgaView three times but if I check the region manager when the window is opened it says that view.count() = 0... I am stumped, the region manager says there are no views but I see three? What am I failing to understand about the region manger? 

Thanks!!!

Dec 4, 2010 at 12:01 PM

What kind of Region are you using? Is it a Region, SingleActiveRegion or AllActiveRegion. Try setting the KeepAlive as false on your views and try.

Also a better approach for a wizard control would be to use a ContentControl/ContentPresenter and setting the Content to the current step view, instead of playing with Regions.