0
votes

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>
1
Why dont you call RegionManager.RegisterViewWithRegion("MainRegion", view), instead of adding the view to the region. It seems to me that it only adds it to the region and not show it. - Rik van den Berg
I tried your suggestion, but I still get a blank window. The view somehow does not end up in the "MainRegion". - Jatin
well it should. You can check this by calling the RegionManager.Regions["MainRegion"] in code or use it in the localwindow by debugging it. Else Try setting a Width and a Height to both the ContentControl and the usercontrol you are loading. It might be possible that it isnt properly sized - Rik van den Berg
The View is there in the regionManager.Regions["MainRegion"].ActiveViews collection and is the only view in that collection. I can confirm that from debugger. So ideally the Text should appear on the screen, but it just doesn't. - Jatin
like I said it must be a styling problem. Your stackpanel (in the window) isnt stretch. try replacing it with a grid. or you can just set the width and height - Rik van den Berg

1 Answers

1
votes

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.