Search code examples
wpfprismprism-4

prism - region not loading contents of usercontrol in shell


I was trying to learn prism, but I am unable to get a very basic example working. I have a Shell which has a single region. I am trying to load a view from module into a shell's region but the shell shows a blank window. I am just not able to figure out why the view is not loaded into the shell's region. Here are the code files.

BootStrapper:

public class Bootstrapper : MefBootstrapper {

    protected override DependencyObject CreateShell() {
        Window shell = new MainWindow();
        Application.Current.MainWindow = shell;
        return shell;
    }

    protected override IModuleCatalog CreateModuleCatalog() {
        return new ConfigurationModuleCatalog();
    }
}

Shell:

<Window x:Class="MVVMPractice.MainWindow">
    <StackPanel x:Name="LayoutRoot" Background="White">
        <ContentControl prism:RegionManager.RegionName="MainRegion" />
    </StackPanel>
</Window>

Module:

[ModuleExport(typeof(CoreModule))]
public class CoreModule : IModule {

    [Import]
    public IRegionManager RegionManager { get; set; }

    public void Initialize() {
        var view = new WelcomeView();
        RegionManager.AddToRegion("MainRegion", view);
    }
}

View To Load in Region:

<UserControl x:Class="MVVMPractice.Modules.Core.WelcomeView">

<Grid Background="Red">
    <TextBlock Text="Hello Prism!" FontSize="20" />
</Grid>
</UserControl>

The above view should show up in the shell, but it doesn't. Any ideas why shell's region does not show the above view ?

EDIT: The Module is configured through the App.config file as given below.

App.config

<configuration>
  <configSections>
    <section name="modules" type="Microsoft.Practices.Prism.Modularity.ModulesConfigurationSection, 
                                  Microsoft.Practices.Prism"/>
  </configSections>
  <modules>
    <module assemblyFile="MVVMPractice.Modules.Core.dll" 
            moduleType="MVVMPractice.Modules.Core.CoreModule, MVVMPractice.Modules.Core.CoreModule, 
                       Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" 
            moduleName="CoreModule" 
            startupLoaded="true" />
  </modules>
</configuration>

Solution

  • This works for me, although I haven't attempted to use app.config configuration yet:

    public class Bootstrapper : MefBootstrapper
    {
        #region Overrides of Bootstrapper
    
        protected override DependencyObject CreateShell()
        {
            return Container.GetExportedValue<MainWindow>();
        }
    
        protected override void InitializeShell()
        {
            base.InitializeShell();
    
            Application.Current.MainWindow = (MainWindow) Shell;
            Application.Current.MainWindow.Show();
        }
    
        protected override IModuleCatalog CreateModuleCatalog()
        {
            return new ConfigurationModuleCatalog();
        }
    
        protected override void ConfigureAggregateCatalog()
        {
            base.ConfigureAggregateCatalog();
    
            AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof (Bootstrapper).Assembly));
            AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(CoreModule).Assembly));
        }
    
        #endregion
    }
    

    // Core module the same as yours

    // Main window (Shell)

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

    Your Xaml works but since you have used a StackPanel the region doesn't fill the view.