5

I've got an little tool similar to the Windows Control Panel. The tool allows us to manage users, configure databases, manage scripts, etc. The home page presents all the sub categories of the application. When you click on a link, it loads the view of this category in the right panel and a small left panel shows the tasks available for this category. Simple.

Basically, what I want to do is to have a "contextalized" status bar. If you are in a view where you need to be connected, the status bar should show you state. If you are in a view where informations should be displayed, I want it in my status bar.

I already put a Region (named StatusBarRegion for the status bar in my shell. For each module, I registered the StatusBarView of this module on the shell's region.

Now, I want to handle the change of context. I need to activate the good view when it's time.

But everytime I try to resolve the StatusBarRegion, it can't be found in the regions of the region manager.

See,

var region = _regionManager.Regions[.RegionNames.StatusBarRegion];
region.Activate(_container.Resolve<StatusBarView>());

The region is always null. Why's that ?

Thanks for your time.

esylvestre
  • 1,850
  • 4
  • 21
  • 30

4 Answers4

3

I believe your error is related to

region.Activate(_container.Resolve<StatusBarView>());

and not

var region = _regionManager.Regions[.RegionNames.StatusBarRegion];

There are a few reasons as to why this could be your problem and I'll give you solutions you could try.

Firstly, region.Activate() requires that the view instance already exists in that region. So from your code, I suspect that _container.Resolve<StatusBarView>() is giving you a new instance of the StatusBarView and therefore will not have existed in that region.

Solution: When you register the StatusBarView with the container, consider a singleton view.

_container.RegisterType<IStatusBarView,StatusBarView>
    (new ContainerControlledLifetimeManager())

Secondly, you must register the view type (or manually add it) to the region before you can activate it.

Solution:

_regionManager.RegisterViewWithRegion
    (RegionNames.StatusBarRegion, typeof(IStatusBarView));

Alternatively:

_regionManager.Regions[RegionNames.StatusBarRegion]
    .Add(_container.Resolve<StatusBarView>());
Tri Q Tran
  • 5,500
  • 2
  • 37
  • 58
1

Double check RegionNames.StatusBarRegion value if already same with region target in your shell.

If it does, region shouldn't be null I think, except you put your handle in your view/viewmodel of module and you hadn't get region manager and container on constructor.

Let say it handled in your view SilverlightUserControl1. The constructor could be like this:

private readonly IRegionManager _regionManager;
private readonly IUnityContainer _container;

public SilverlightUserControl1(IRegionManager regionManager, IUnityContainer container)
{
    _regionManager = regionManager;
    _container = container;
}

private Button1_Click(object sender, RoutedEventArgs e)
{
    var statusBarView = _container.Resolve<StatusBarView>();
    statusBarRegion = _regionManager.Regions["StatusBarRegion"];

    statusBarRegion.Add(statusBarView, "StatusBarView");
    statusBarRegion.Activate(statusBarView);

    // or you could remove all views in `ActiveViews` and add the view then
    // (no need to activate)
}
Jeaf Gilbert
  • 11,495
  • 19
  • 78
  • 105
  • Thank you for your time, it's been appreciated. But like I said previously, the region is null because the code was in the Initialize method of the Module, so the UI was not created yet, so the region could not be resolved. This said, your answer gives a good example of the proper manner to use regions. Thanks again. – esylvestre Jun 03 '10 at 15:55
  • Is there any advantage in deactivating and activating views over using `RemoveAll()` followed by adding the desired view? Thanks. – W.M. Mar 24 '17 at 14:20
1

I've had a similar problem a while back. I've posted a question here, but then figured out the problem and its solution.

My problem was that my region wasn't defined in the Shell. Check out the full question and answer here.

I hope this helps.

Community
  • 1
  • 1
AbdouMoumen
  • 3,814
  • 1
  • 19
  • 28
0

The reason why the region was null ? The piece of code was in the Initialize method of the Module, so the UI was not created yet.

For the best way to manage my status bars, I'm still wondering how I'm going to do it.

esylvestre
  • 1,850
  • 4
  • 21
  • 30
  • According to the PRISM team, module initialization will occur after the shell has been initialized. See here: http://msdn.microsoft.com/en-us/library/gg430868(v=pandp.40).aspx so your assumption that the UI not being created, I believe is incorrect. – Tri Q Tran Dec 16 '10 at 23:04