0

I have a scenario. I am writing a WPF app using Prism 6.0, where I want to POP up a Child Window first that will have three buttons for three different UI design. Similar like this. enter image description here

Based upon the Selection I will update the MainWindowViewModel and will close the Child Window, and show the MainWindow.

Until This part is good. But the problem is after this part, The three different buttons points to three different UI designs. Specially the ContentRegion1 and ContentRegion2. These two regions are different.

I have seen that if I put a command through a Button then this code runs successfully. But the same doesn't run if I put that in MainWindowViewModel.

public MainWindowViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
{
    _regionManager = regionManager;
    _eventAggregator = eventAggregator;
    _regionManager.RequestNavigate("ContentRegion1", "firstUiDesign");

...

}

The MainWindowlooks like this ...

enter image description here

ContentRegion1 and ContentRegion2 are two designed in XAML in this way

<Border CornerRadius="15" Grid.Column="0">
   <StackPanel>
        <ContentControl prism:RegionManager.RegionName="ContentRegion1" />
   </StackPanel>
</Border>
<Border CornerRadius="15" Grid.Column="1">
   <StackPanel Grid.Column="1" Margin="2">
        <ContentControl prism:RegionManager.RegionName="ContentRegion2" />
   </StackPanel>
</Border>

However I am unable to figure out what I did wrong or what Extra thing I need to put into the code to make it work.

Even in the BootStrapper.cs also I have this code

BootStrapper Code:

protected override DependencyObject CreateShell()
{
   //return base.CreateShell();
   return Container.Resolve<MainWindow>();
}

protected override void InitializeShell()
{          
    Application.Current.MainWindow.Show();
}

protected override void ConfigureContainer()
{
    base.ConfigureContainer();
    Container.RegisterTypeForNavigation<TestUserControl>("firstUiDesign");

}

Can anybody help in this.

Debhere
  • 1,055
  • 2
  • 19
  • 41
  • So your issue is that you can't display the firstUiDesign view in ContentRegion1? How is your bootstrapper implemented? – mm8 Jun 27 '17 at 10:47
  • I have updated my source code with Bootstrapper code – Debhere Jun 27 '17 at 11:33
  • And where are you creating the MainWindowViewModel? Are you using the ViewModelLocator to create it for you? – mm8 Jun 27 '17 at 11:46
  • I have used the default style of Prism `xmlns:prism="http://prismlibrary.com/" prism:ViewModelLocator.AutoWireViewModel="True"` – Debhere Jun 27 '17 at 12:18
  • 1
    That's your issue. You cannot call the RequestNavigate method before the regions have been created. Please see my answer. – mm8 Jun 27 '17 at 12:38

1 Answers1

1

Don't use the ViewModelLocator to create the MainWindowViewModel. Create it yourself in the Bootstrapper after the MainWindow and the regions have been created:

protected override DependencyObject CreateShell()
{
    return Container.Resolve<MainWindow>();
}

protected override void InitializeShell()
{
    var mainWindowViewModel = Container.Resolve<MainWindowViewModel>();
    Application.Current.MainWindow.DataContext = mainWindowViewModel;
    Application.Current.MainWindow.Show();
}

protected override void ConfigureContainer()
{
    base.ConfigureContainer();
    Container.RegisterTypeForNavigation<TestUserControl>("firstUiDesign");
}

Remove this from MainWindow.xaml:

prism:ViewModelLocator.AutoWireViewModel="True">
mm8
  • 163,881
  • 10
  • 57
  • 88
  • This is a nice solution, I will try , but currently i have solved it in the following way. In the MainWindowViewModel I have the below code in the Constructor `this._regionManager.RegisterViewWithRegion("ContentRegion1", typeof(firstUiDesign));` This way it's showing the FirstUiDesign at run-time. Anyways Thanks for pointing out the Prism ViewModelLocator – Debhere Jun 27 '17 at 13:08