Multiple instances create for same region

Topics: Prism v4 - WPF 4
Mar 7, 2011 at 4:21 AM

Hi,

I have a UserControl which consists of a region name "MainSearchGridPreviewPane", which serve as "container" to receives view injected by other modules.

I need to open that UserControl multiple times in a TabControl, and I expect it to show in different tabs, this part work well without region added.

Refer to this article, I try to use the Scoped Region since I think multiple region with same name will be call later, I've added the region into XAML file, and have the code below when UserControl loaded:

XAML in UserControl

<ItemsControl Grid.ColumnSpan="6" Grid.Row="4" Margin="37,9,30,12" Name="PreviewPane" Regions:RegionManager.RegionName="MainSearchGridPreviewPane" />

Code in ViewModel

private void ViewLoaded(EventParameters eventParameters)

        {
// View injection
var searchBarView = _container.ResolveViewAndViewModel<NormalSearchView, NormalSearchViewModel>();
IRegion previewPaneRegion = this._regionManager.Regions["MainSearchGridPreviewPane"];

IRegionManager previewPaneRegionManager = previewPaneRegion.Add(searchBarView, null, true);
//previewPaneRegion.Activate(searchBarView);
}

The UserControl able to show without error at first time, however when I try to open the same UserControl second time at second tab, I got the error message below:

 

Microsoft.Practices.Prism.Regions.UpdateRegionsException was unhandled by user code
  Message=An exception occurred while trying to create region objects.
    - The most likely causing exception was: 'System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: Region with the given name is already registered: MainSearchGridPreviewPane
   at Microsoft.Practices.Prism.Regions.RegionManager.RegionCollection.Add(IRegion region)
   at Microsoft.Practices.Prism.Regions.Behaviors.RegionManagerRegistrationBehavior.TryRegisterRegion()
   at Microsoft.Practices.Prism.Regions.Behaviors.RegionManagerRegistrationBehavior.OnUpdatingRegions(Object sender, EventArgs e)
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
   at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Delegate.DynamicInvokeImpl(Object[] args)
   at System.Delegate.DynamicInvoke(Object[] args)
   at Microsoft.Practices.Prism.Events.WeakDelegatesManager.Raise(Object[] args)
   at Microsoft.Practices.Prism.Regions.RegionManager.UpdateRegions()'.
    But also check the InnerExceptions for more detail or call .GetRootException().

 

Any idea for that? Please tell me if my concept of region is wrong because I still new to MVVM/Prism.

 

Thanks in advance.

Mar 7, 2011 at 6:06 AM

I've found the reason, it is because the UserControl is inject into another region call "WorkRegion", which have the code to inject the view (UserControl in this case) into WorkRegion.

I just modified the view injection code for WorkRegion to generated a scoped region when the view (The UserControl) is injected into tab, then the error disappear!

 

But now I need to think about the retrieval of scoped region across modules...

 

Thanks everybody for spending time on this thread. :)

Developer
Mar 9, 2011 at 4:29 PM

Hi,

Thank you for sharing your insight with the rest of the community, as other users might benefit from reading this in case a similar issue arises.

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