1
votes

I've seen some examples on changing the user control in only one window using Prism for WPF and it looks like this:

Bootstrapper.cs

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

        Container.RegisterType(typeof(object), typeof(ViewA), "ViewA");
        Container.RegisterType(typeof(object), typeof(ViewB), "ViewB");
    }

MainWindowViewModel.cs

    public class MainWindowViewModel : BindableBase
    {
        private readonly IRegionManager _regionManager;

        public DelegateCommand<string> NavigateCommand;


        public MainWindowViewModel(IRegionManager regionManager)
        {
            _regionManager = regionManager;

            NavigateCommand = new DelegateCommand<string>(ExecuteNavigateCommand);
        }

        private void ExecuteNavigateCommand(string uri)
        {
            _regionManager.RequestNavigate("ContentRegion", uri);
        }
    }

MainWindow.xaml

<Button Grid.Row="1" Grid.Column="1" Command="{Binding NavigateCommand}" CommandParameter="ViewA" FontSize="16" Content="View A" Margin="4"/>
<Button Grid.Row="1" Grid.Column="2" Command="{Binding NavigateCommand}" CommandParameter="ViewB" FontSize="16" Content="View B" Margin="4"/>

You can click it and the views will change. But when you start it, there is no user control loaded, only the main window. My question is How can you load a user control to the MainWindow on start of the application?

1

1 Answers

1
votes

You have different options here. First of all, register the view for the region so that it is automatically discovered and displayed. That works if you don't want to navigate to that view afterwards.

_regionManager.RegisterViewWithRegion("MyRegion", typeof(ViewA));

Alternatively, you can navigate to the view when the application is done with starting. That is, from the end of Bootstrapper.InitializeModules. If the bootstrapper doesn't know of the view or you want to do other things at this moment, too, you can also publish an event like ModulesInitialized and let the module defining your view subscribe to that event.

// in the assembly defining the interfaces shared between your modules
public class ModulesInitialized : PubSubEvent {}

// in the bootstrapper.InitializeModules
Container.Resolve<IEventAggregator>().GetEvent<ModulesInitialized>().Publish();

// in the module defining viewA
_eventAggregator.GetEvent<ModulesInitialized>().Subscribe( () => _regionManager.Requestnavigate( "MyRegion", "ViewA" ), true );