Unable to create a region

Topics: Prism v2 - Silverlight 2
May 5, 2009 at 8:20 AM
Hello,

I'm developing a Silverlight 2.0 composite application using your framework. Unfortunately, I didn't manage to achieve an apparently simple talk - I'm probably missing something huge here.

Basically, I have a main region in my shell view, and a module that inject a view in this region. It works fine. Now, I want this module view to contain another region, in which I would like to inject another view, defined in the same module.

So:
  • In the first module view, I declared another named region
  • In the module entry point, I bound another view type to this region name
But it doesn't work. My first view is showing well in the shell view like before, but the new view doesn't show up in the new inner region. If I pass the region manager to the module view and inspect it, it seems that the region doesn't exist. I also tried to register the region programmatically, using RegionManager.SetRegionName and RegionManager.SetRegionManager static methods, instead of using the dependency property in xaml, but with no success.

Any ideas?

Thank you very much.

May 5, 2009 at 10:26 PM

Hi Romain,

 

Let’s say ViewA is the view added to the MainRegion and let’s call the view that will be added to ViewA's region ViewB. The RegionManager will recognize ViewA’s region (“NewRegion”) only after ViewA has been added to a region.

I have tried reproducing your error in the following ways with no success with the following code in the Initialize() method of a module:

 

//using view injection for ViewA without a scoped region and view Discovery for ViewB

public void Initialize()

  {

    regionManager.Regions["MainRegion"].Add(new ViewA());

    regionManager.RegisterViewWithRegion("NewRegion", typeof(ViewB));

  }

 

// using view injection for ViewA with a scoped region and view discovery for ViewB

public void Initialize()

  {

    regionManager.Regions["MainRegion"].Add(new ViewA(), "", true);

    regionManager.RegisterViewWithRegion("NewRegion", typeof(ViewB));

  }

 

// using view discovery for both views. ViewA first, ViewB after.

public void Initialize()

  {

    regionManager.RegisterViewWithRegion("MainRegion",typeof(ViewA));

    regionManager.RegisterViewWithRegion("NewRegion", typeof(ViewB));

  }

 

// using view discovery for both views. ViewB first, ViewA after.

public void Initialize()

  {

    regionManager.RegisterViewWithRegion("NewRegion", typeof(ViewB));

    regionManager.RegisterViewWithRegion("MainRegion",typeof(ViewA));

  }

 

I didn't try to inject ViewB into region "NewRegion" via view injection because it would throw an exception if the "NewRegion" was not in the RegionManager. However, this seems not to be your case as you are not receiving any exception.

Could you provide more information about the code you are using?

 

Below you can find topics from the Composite Application Guidance for WPF & Silverlight documentation. These topics are related to some of the things I mentioned above:

·         How to: Show a View in a Region Using View Discovery UI Composition

·         How to: Show a View in a Region Using View Injection UI Composition

·         How to: Show a View in a Scoped Region

·         UI Composition (technical concept)

 

Please let me know if this helps.

 

Damian Schenkelman

http://blogs.southworks.net/dschenkelman

May 6, 2009 at 2:30 PM
Hi Damian,

Thank you for the answer. I'm using the third approach you described, registering both views in the module entry point.

In ViewA, I declared the region using the following XAML snippet:

<ItemsControl x:Name="newRegionPanel"  Regions:RegionManager.RegionName="NewRegion"/>

Something bizarre: I got the region manager instance injected via the ViewA constructor, and kept a reference to it in a private instance field. Then, if I I add a button in ViewA, and try to execute the following code in the button click handler, I effectively get an exception noticing me that the region "NewRegion" doesn't exist. MainRegion can be found:

            var mainRegion = this.regionManager.Regions["MainRegion"]; // OK
            var newRegion = this.regionManager.Regions["NewRegion"]; // Throws

Side note: I'm using a custom bootstrapper, which I implemented using Ninject as IoC framework. Is there a risk that I forgot something here? Would sounds weird thought, since the MainRegion seems to work fine, as well as others CAL features so far.

Do you have any other ideas?

Thank you very much!
May 7, 2009 at 7:42 PM

Hi Romain,

 

I have tried to reproduce your issue following the specifics you provided (using an ItemsControl as a region and trying to get the regions from a View button) and was not able to find an error. Although I do not know the specifics of your application, this could be related to the fact that you are using Ninject and had to change the UnityBootstrapper.

 

A way to try to find the possible error could be:

1.       Creating a simple sample that reproduces your scenario, using the code from the default CAL (with Unity as the container).

2.       Debug the sample and check that it works.

3.       Debug your solution and find the differences with the sample.

 

In case the previous steps do not work out and it suits your application needs, you could try adding the view in a Scoped Region. It is not the same situation as what you were trying to do, but it might get your view injected into the “NewRegion”.

 

Please let me know if this helps.

 

Damian Schenkelman

http://blogs.southworks.net/dschenkelman