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.