Value does not fall within the expected range

Topics: Prism v4 - Silverlight 4
Jul 7, 2011 at 2:54 PM

I'm trying to accomplish the following, but I think that my understanding as to how MEF and PRISM share controls is a bit skewed.  For the sake of brevity, I have created fictitious controls to hit my point.

 

Host.xaml - this view hosts an area where other fully functional views are plugged into

Within Host.xaml....

<ContentControl regions:RegionManager.RegionName="mainRegion" />

 

Employees.xaml - loaded into mainRegion upon link request

Within Employees.xaml....

 

<ContentControl regions:RegionManager.RegionName="empAddressRegion" />

 

Customers.xaml - loaded into mainRegion upon link request

Within Customers.xaml....

 

<ContentControl regions:RegionManager.RegionName="custAddressRegion" />

 

 Addresses.xaml - loaded into empAddressRegion or custAddressRegion when Employees.xaml or Customers.xaml is loaded into mainRegion on Host view, respectively.



 Here is the class definition: 

    [Export]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public partial class Addresses : UserControl

So what I'm trying to achieve here is that I want the Addresses view to be loaded once, and once only, and used by other dynamically loaded content.

When the app first loads, I can load Employees into mainRegion, and Addresses will load fine into empAddressRegion.  I am performing the loading of the Addresses view into empAddressRegion via the following statement: 

	// Code within Employees code behind
        public void OnImportsSatisfied()
        {   
            RegionManager.RegisterViewWithRegion("empAddressRegion"typeof(Addresses));
        }

However, when I attempt to load Customers into mainRegion, it fails with the "Value does not fall within expected range" exception when it attempts to load Addresses into custAddressRegion via the following statement:

	// Code within Customers code behind
        public void OnImportsSatisfied()
        {
            RegionManager.RegisterViewWithRegion("custAddressRegion"typeof(Addresses));
        }
 
 

It should also be noted that if Customers is loaded first, then the loading of Employees fails with the same exception.

Again, the controls and their intent are fictitious, so I'm not after whether the Addresses control should be shared. 

In my application, I do have a control that loads quite a bit of data that needs to be displayed on multiple controls that are dynamically loaded upon request, and I really want it to be loaded once and reused throughout.

Given my scenario, is it possible to achieve the desired behavior based on my layout?

If so, what am I doing wrong?

 

 

Jul 7, 2011 at 3:57 PM

Hi,

You might find the following thread handy where similar behaviors using MEF are covered:

RegionCreationException when opening page second time

In case you continue experiencing this problem, it could be helpful if you could provide us with a repro sample (for example, uploading to SkyDrive) of your solution, so that we can help you find out the cause.

Thanks,

Miguel Bronzovic
http://blogs.southworks.net/mbronzovic

 

Jul 7, 2011 at 4:35 PM

Hi Miguel, thanks for the response.  I had read that post you suggested before posting myself, however, it is not the same issue. 

My issue is a bit different, in that I have a control that I want to be shared by other controls that are dynamically loaded. 

The first dynamically loaded control that needs the shared control is able to load the shared control. 

Any further dynamically loaded content who needs access to the shared control fails.

Is this a limitation of PRISM and the RegionManager?  Can I truly share a control the way I have described?

 

Much thanks!

Developer
Jul 7, 2011 at 6:04 PM

Hi,

It could be helpful to know that a single instance of a control cannot be added to the visual tree twice. Therefore, if you wish to share the same instance of a control between different regions, you should remove it from one region before adding it to the other one.

This happens because of the way Silverlight works, not due to a Prism limitation.

I hope you find this helpful.

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

Jul 8, 2011 at 1:59 AM

Guido, thanks much, that answers my question.