Views with child/nested views

Topics: Prism v4 - Silverlight 4
Nov 2, 2010 at 1:13 PM

Hi,

I am relatively new to Prism and using prism 4 drop 10 and MEF.  Can some one help me find a way to create views that itself comprised of child views.

when I add a region name i Get an exception while trying to create the parent view.

This is what I tried on my First view that is on the shell:

<Border x:Name="EditGrid" Grid.Row="2" Grid.Column="2" BorderThickness="2" BorderBrush="#99FFFFFF" CornerRadius="10"Margin

="3" Regions:RegionManager.RegionName="MasterDataEditRegion"/>

 

I spent a while searching on the net, but didn't find any example on how this can be done.. 

 Is this something we can do in the Prism ?

Regards, Kiran

Nov 2, 2010 at 6:43 PM

Hi Kiran,

Nice to see that you are using Prism 4. To expose a UI control as a region (Border), it is necessary to use a region adapter, since they are responsible for the creation of the region and associating it to the control. Prism provides you with three region adapters out-of-the-box:

  • ContentControlRegionAdapter. This adapter adapts controls of type System.Windows.Controls.ContentControl and derived classes.
  • SelectorRegionAdapter. This adapter adapts controls derived from the class System.Windows.Controls.Primitives.Selector, such as the System.Windows.Controls.TabControl control.
  • ItemsControlRegionAdapter. This adapter adapts controls of type System.Windows.Controls.ItemsControl and derived classes.

Since the Border control does not derive from any aforementioned UI Control, you can not expose it as a region without create a custom region adapter. Otherwise, you will receive an exception. If you want to create a custom region adapter, you could take a look at the following documentation section on MSDN: How to: Create a Custom Region Adapter

Hope this helps.

Fernando Antivero
http://blogs.southworks.net/fantivero

 

Nov 2, 2010 at 7:24 PM
Edited Nov 3, 2010 at 2:58 AM

Fernando Antivero,

Thanks, I just managed to fingure that out :).

may be you can help me out with the next issue I am having with the view discovery. (After the adding the child region and trying to register a view with this)

In my code now when I have a code like "iRegionManager.RegisterViewWithRegion("MasterDataEditRegion", typeof(EquipmentsView));"  I get an exception

'Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type EquipmentsView, key "" ---> Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type EquipmentsView, key ""
   at Microsoft.Practices.Prism.MefExtensions.MefServiceLocatorAdapter.DoGetInstance

I have the PartCreationPolicy set on my view class.

however the following code just works fine:

EquipmentsView view = new EquipmentsView();
iRegionManager.AddToRegion("MasterDataEditRegion", view);
iRegionManager.Regions["MasterDataEditRegion"].Activate(view);

Am I missing something here?

Regards, Kiran 

Nov 2, 2010 at 7:59 PM
Edited Nov 2, 2010 at 9:28 PM

Hi,

I am glad that you found a solution for your scenario with Regions. Regarding to your second thread, in your second approach you are using View Injection instead of View Discovery, and you do not use the Dependency Injection Container (MEF) to create an instance of your view. This is why, you are not receiving an error.

If your scenario requires to use View Discovery, you have to two options to get this working:

Option 1:

You have to decorate your view class with the Export attribute (pseudo-code):

[Export]
public class EquipmentsView
{
...
}

Then you have to register the EquipmentsView in your MasterDataEditRegion using the following code (pseudo-code):

iRegionManager.RegisterViewWithRegion("MasterDataEditRegion", typeof(EquipmentsView));

Option 2:

You could decorate your view class using the ViewExport attribute. This attribute allows you to indicate the region where this view will be registered. This way, you save a code line for registering your view in the region and also provides your a more MEFEE code. So, you could use the following code (pseudo-code):

[ViewExport(RegionName = "MasterDataEditRegion")]
public class EquipmentView
{
...
}

If you need more guidance on this topic you could take a look at the Stock Trader Reference Implementation Application.

Hope this helps.

Fernando Antivero
http://blogs.southworks.net/fantivero

Nov 3, 2010 at 3:04 AM

Fernando Antivero,

Thanks. This is what I was missing.

Regards,Kiran

Feb 6, 2011 at 11:49 AM
Edited Feb 6, 2011 at 11:52 AM

That helps me as well ...

regards