views:

113

answers:

3

I'm using PRISM in a SilverLight 4 application. I have a problem where views registered to some regions doesn't get displayed.

When loading a module at startup I register some views to regions as follows:

RegionManager.RegisterViewWithRegion("MyRegion1", typeof(IMySubView1));
RegionManager.RegisterViewWithRegion("MyRegion2", typeof(IMySubView2));

I have a view implementing an interface called IMyView, where the xaml have two contentcontrols with regions defined in a grid like this:

<ContentControl Regions:RegionManager.RegionName="MyRegion1" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Grid.Row="0" Grid.RowSpan="1"/>
<ContentControl Regions:RegionManager.RegionName="MyRegion2" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Grid.Row="1" Grid.RowSpan="1"/>

I have tried two different methods for adding the view to the main region. Both adds the view and basic elements such as buttons get displayed, but the regions defined in the view does not get filled with associated views.

Method 1:

object obj = _container.Resolve<IMyView>();
IRegion mainRegion = _regionManager.Regions["MainViewRegion"];
IRegionManager scoped = mainRegion.Add(obj, "test", true);
mainRegion.Activate(obj);

// Enabling the following call, it will fail saying the region MyRegion1 does not exist. Feels like it should?
// IRegion myRegion = scoped.Regions["MyRegion1"];

Method 2:

object obj = _container.Resolve<IMyView>();
_regionManager.AddToRegion("MainViewRegion", obj);
_regionManager.Regions["MainViewRegion"].Activate(obj);

It feels like the regions defined in the xaml file doesn't get registered, and because of that the registered views do not get displayed.

The MainViewRegion is defined in the shell in a TabControl as this:

<TabControl Margin="8,0,8,8" Regions:RegionManager.RegionName="MainViewRegion">

Any suggestions on solving my problem will be greatly appreciated!

A: 

Hi,

I'm facing the same problem. The idea is that for whatever reason view injection {mainRegion.Add(obj, "test", true)} for already created regions doesn't show the view. A workaround that worked for me, is to create the region from code, and then inject the view. Something like this:

Microsoft.Practices.Composite.Presentation.Regions.RegionManager.SetRegionManager(headerRegionContainer, _RegionManager); Microsoft.Practices.Composite.Presentation.Regions.RegionManager.SetRegionName(headerRegionContainer, regionName);

var view = _UnityContainer.Resolve(bag.HeaderViewType); _RegionManager.Regions[regionName].Add(view); _RegionManager.Regions[regionName].Activate(view);

Unfortunately for me I can't reach my goal this way, but maybe you can.

Regards,

Levente

Halász Levente
Thanks for replying.Unfortunately it didn't help, my problem behaved exactly the same efter trying your suggestion.
asdf
A: 

After many hours of troubleshooting I found something.

In Composite.Presentation\Regions\RegionManager.cs there is a method called IsInDesignMode. When a region is about to be created this method is called, and if this method returns true the region is not created. See below:

private static void OnSetRegionNameCallback(DependencyObject element, DependencyPropertyChangedEventArgs args)
{
    if (!IsInDesignMode(element))
    {
        CreateRegion(element);
    }
}

private static bool IsInDesignMode(DependencyObject element)
{
    // Due to a known issue in Cider, GetIsInDesignMode attached property value is not enough to know if it's in design mode.
    return DesignerProperties.GetIsInDesignMode(element) || Application.Current == null
           || Application.Current.GetType() == typeof(Application);
}

When our silverlight application starts up and the regions in the shell gets created everything is fine, the Application.Current property is of type "MyName.Shell.App". But when a view gets added after startup, as a response to user input, the Application.Current type is suddenly of type "Application", and thus the IsInDesignMode method returns true and the regions are not created.

If I remove the Application.Current conditions everything works as expected. So the question is, is there something wrong in my application or is there something wrong in the prism source code?

asdf
A: 

where is your _regionManager coming from ? Did you write a proper BootStrapper ? You need to write a class inheriting from MefBootstrapper or UnityBootstrapper (or a custom one if you're not using neither of those IoC/Extension framework) in order to register all your needed types within the IoC container.

could you post the BootStrapper code ?

Salfab
yes I have a proper bootstrapper. see my next post, when I made a small modification to the prism source code everything started to work as expected. I'm having a dialog with the prism team regarding my problem to figure out the root cause.
asdf
make sure to to post the outcome of this dialog, as others might run into the same issue :)
Salfab
yes, will do :)you can follow the dialog on http://compositewpf.codeplex.com/Thread/View.aspx?ThreadId=222920
asdf