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.
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>());
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)
}
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.
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.
精彩评论