0

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.

2
  • 1
    Which File in your solution does contain the code?
    – Nikolaus
    Sep 11, 2018 at 7:29
  • @Nikolaus ViewModels → ViewINjectionMainViewModel.cs or build → Menubar → "Views/Region" → "0. VIew injection" Thanks Sep 11, 2018 at 7:42

2 Answers 2

3
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

2
  • 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. Sep 12, 2018 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. Sep 12, 2018 at 7:44
0

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.

1
  • 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, 2018 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.