I need help with an unexpected exception "RegionCreationException"

Topics: Prism v4 - WPF 4
Jul 21, 2011 at 3:24 PM

Hi there,

I’m creating an application that uses a wizard.

The wizard is a new Window with its own region named “WizardStepsRegion” where all the steps (views) will be shown there and it has a button that when is pressed it shows a new UserControl (the step 2 of the wizard).

It works well but when I close the wizard window and open it again without closing the application I get the following exception 

“RegionCreationException”. An exception occurred while creating a region with name 'WizardStepsRegion'. The exception was: System.ArgumentException: Region with the given name is already registered: WizardStepsRegion.

I’ve created a new simplified application that shows the same error I tried to keep it as simple as possible using the MVVM pattern only in the wizard to simplify the code. The solution can be downloaded from this link https://skydrive.live.com/?cid=737f94b0dcbc9094&sc=documents&id=737F94B0DCBC9094%212189

The app is composed of two Windows the shell and the wizard. When you run it you'll see the shell with a button "push me" and when you push it the wizard is opened.

Can someone help me to fix this and tell me why I get that exception the second time I open the Window?

Thank you very much.

Developer
Jul 21, 2011 at 6:39 PM
Edited Jul 21, 2011 at 6:39 PM

Hi,

We've examined your sample and found that the problem is happening because the region is being registered in the region manager each time you show your wizard. As a workaround to prevent that, we've added the following line in the handler for the Closed method of your Wizard dialog:

 

ServiceLocator.Current.GetInstance<IRegionManager>().Regions.Remove("WizardStepsRegion");

 

However, please note that in order to show views in a region inside a popup dialog, the recommended approach is to use the RegionPopupBehaviors, as in the Stock Trader Reference Implementation. You can find more information about it in this thread:

Modal Dialog

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Jul 21, 2011 at 8:27 PM

Hi Guido,

Thank you very much for your help. I tried it and now it works :-)

I spent some time looking for a way to remove the region but I failed so I supposed that there was no way to remove a region and I thought that the region is removed once it has no views but I removed the views and the error persisted so I didn't know what to do.

Thank you for your help. I'll check now how work the RegionPopupBehaviors.

Thank you.