Prism error 'The region manager does not contain the region'
Prism error 'The region manager does not contain the region'
enter image description here
I am developing a Prism Sample application with WPF following MVVM pattern, but it has some problem in my application when it pass below code :
var view = _container.Resolve<DummyView>();
IRegion region = _regionMansger.Regions["ViewInjectionMain_MainRegion"];
I have tried out the solution proposed in other stackoverplow posts but it doesn't work.
(project github here).
class ViewInjectionMainViewModel : BindableBase
IUnityContainer _container;
IRegionManager _regionMansger;
public ViewInjectionMainViewModel(IUnityContainer container, IRegionManager regionManager)
_container = container;
_regionMansger = regionManager;
var view = _container.Resolve<DummyView>();
IRegion region = _regionMansger.Regions["ViewInjectionMain_MainRegion"];
region.Add(view);
class Bootstrapper : UnityBootstrapper
protected override DependencyObject CreateShell()
return this.Container.Resolve<MainWindow>();
protected override void InitializeShell()
base.InitializeShell();
var regionManager = this.Container.Resolve<IRegionManager>();
if (regionManager != null)
regionManager.RegisterViewWithRegion("MainRegion", typeof(FirstView));
Container.RegisterType<object, RegionBasic>("RegionBasic");
Container.RegisterType<object, RegionControlMain>("RegionControlMain");
Container.RegisterType<object, DummyView>("DummyView");
Container.RegisterType<object, ViewInjectionMain>("ViewInjectionMain");
Application.Current.MainWindow.Show();
protected override void ConfigureModuleCatalog()
var catalog = (ModuleCatalog)ModuleCatalog;
catalog.AddModule(typeof(ModuleA.ModuleAModule));
catalog.AddModule(typeof(ModuleB.ModuleBModule));
catalog.AddModule(typeof(DummyModule.DummyModuleClass));
Thanks.
@Nikolaus ViewModels → ViewINjectionMainViewModel.cs or build → Menubar → "Views/Region" → "0. VIew injection" Thanks
– Taekyung Lee
Sep 11 '18 at 7:42
2 Answers
2
regionManager.RegisterViewWithRegion("MainRegion", typeof(FirstView));
regionManager.RegisterViewWithRegion("ViewInjectionMain_MainRegion", typeof(DummyView)); //add this line
and get region via Dispatcher (the ViewInjectionMain.xaml should be loaded for loading ViewInjectionMain_MainRegion also)
class ViewInjectionMainViewModel :BindableBase
IUnityContainer _container;
IRegionManager _regionMansger;
public ViewInjectionMainViewModel(IUnityContainer container, IRegionManager regionManager)
_container = container;
_regionMansger = regionManager;
System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
var view = _container.Resolve<DummyView>();
IRegion region = _regionMansger.Regions["ViewInjectionMain_MainRegion"];
region.Add(view);
));
Hope this will help you
It works! welll, why does it need Dispatcher for "the ViewInjectionMain.xaml should be loaded for loading ViewInjectionMain_MainRegion also" can you tell me more detail? Thanks.
– Taekyung Lee
Sep 12 '18 at 1:01
I am not sure but I think that as your ViewInjectionMain.xaml was not loaded until you call _regionManager.RequestNavigate("MainRegion", viewName); so this region ("ViewInjectionMain_MainRegion") can not be loaded into regions. while you load ViewInjectionMain.xaml your GUI was not loaded yet because GUI can be loaded when your function call ended, so you need the Dispatcher to be sure that ViewInjectionMain.xaml was loaded in GUI.
– Ilya Grigoryan
Sep 12 '18 at 7:44
I tested follow advice. I made some button and bound it to "DelegateCommand". It works. but when i made some function that "IRegion region = _regionMansger.Regions["ViewInjectionMain_MainRegion"];" and it call in constructor it doesn't work.
RegionManager contain region after view's viewmodel constructor ended.
So how did you fix this so that the function was called after the constructor? Of course a button is possible but I need it to run directly when available.
– nldev
Nov 22 '18 at 12:55
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Which File in your solution does contain the code?
– Nikolaus
Sep 11 '18 at 7:29