0
votes

I am creating a WPF MVVM Prism application where I need to switch between views within a region, which I do with view injection. I instantiate the Region in MainWindow.xaml like this:

<ContentControl prism:RegionManager.RegionName="ContentRegion" />

Changing the views with commands later is no problem. The problem is that I need the region at program start to be able to show a view from the beginning. When I put that in the constructor like

public MainWindowViewModel(IContainerExtension container, IRegionManager regionManager)
{
   _regionManager = regionManager;
   _container = container;

   _view1 = _container.Resolve<1Control>();
   _view2 = _container.Resolve<2Control>();
   _contentregion = _regionManager.Regions["ContentRegion"];
   _contentregion.Add(_view1);
   _contentregion.Add(_view1);
}

I get the error that RegionManager does not contain ContentRegion. This is the case because it is not yet instantiated from XAML at that time. I cannot use the main window and the Loaded event like here, because I need to do it in the view model, instead of code-behind. It would also be enough to have a default view set somehow (in XAML Maybe?) and then later change the active view with commands, but I also don't see how that could work.

1
Please consider reading our How to Ask topics. Your question is hard to read and is lacking the relevant code (to understand your setup). Please re-format your question and add the missing information. - dymanoid
you can do _regionManager.RegisterViewWithRegion(regionName, viewType); - Athul Raj
RegionManager sounds like something that needs to be initialized in App.xaml.cs, before any view or viewmodel are loaded. - o_w

1 Answers

0
votes

If you want view injection or use the navigation framework and need to set an initial view, you can inject it or navigate to it using the region manager in the Prism application by overriding OnInitialized in App.xaml.cs. It is important that you call base.OnInitialized(); first, because it will call Show() on the shell window. Consequently, after this call the shell view is initialized and the region manager knows about its regions.

public partial class App : PrismApplication
{
   // ...bootstrapping code.

   protected override void OnInitialized()
   {
      base.OnInitialized();

      var regionManager = Container.Resolve<IRegionManager>();
      var contentRegion = regionManager.Regions["ContentRegion"];

      var view1 = Container.Resolve<Control1>();
      var view2 = Container.Resolve<Control2>();
      contentRegion.Add(view1);
      contentRegion.Add(view1);
   }
}

Another approach is to use view discovery instead of view injection, where you register the view to a region with region manager using the RegisterViewWithRegion method. In your Prism application in App.xaml.cs, you can do that in different places like CreateShell or OnInitialized, because this method only registers the view with the region. It will be instantiated later when the region is available and the view is shown. Prism will create the view automatically and load it into the region.

protected override void OnInitialized()
{
   var regionManager = Container.Resolve<IRegionManager>();
   regionManager.RegisterViewWithRegion("ContentRegion", typeof(Control1));
   regionManager.RegisterViewWithRegion("ContentRegion", typeof(Control2));
}