Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prism : Change the active view

Tags:

c#

wpf

region

prism

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.

like image 945
esylvestre Avatar asked May 27 '10 16:05

esylvestre


1 Answers

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>());
like image 166
Tri Q Tran Avatar answered Sep 26 '22 02:09

Tri Q Tran