0

Im trying to create a new WPF MVVM prism project, but when i try to add view to my region in shell there is a null reference exception for RegionManager property. Can anybody help me ? here is my code:

 class Bootstrapper : UnityBootstrapper
    {
        public IRegionManager regionManager { get; set; }
        public IRegionManager RegionManager
        {
            get
            {
                return regionManager;
            }
            set
            {
                regionManager = value;
            }
        }

        protected override DependencyObject CreateShell()
        {
            return new Shell();
        }

        protected override void InitializeShell()
        {
            base.InitializeShell();
            App.Current.MainWindow = (Window)this.Shell;
            App.Current.MainWindow.Show();
            this.RegionManager.RequestNavigate("MainRegion", new Uri("PartNumberView", UriKind.Relative));
        }

        protected override void ConfigureModuleCatalog()
        {
            base.ConfigureModuleCatalog();
        }
    }

shell xaml:

<Window x:Class="MechanicsPriceComparer.Shell"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:prism="http://www.codeplex.com/prism"
        xmlns:local="clr-namespace:MechanicsPriceComparer"
        mc:Ignorable="d"
        Title="MechanicPriceComparer" Height="450" Width="800">
    <Grid>
        <ItemsControl Name="NameOfMainRegion" prism:RegionManager.RegionName="MainRegion" ></ItemsControl>
    </Grid>
</Window>

app.xaml:

protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            Bootstrapper bootstrapper = new Bootstrapper();
            bootstrapper.Run();
        }
Genna
  • 1
  • 5

1 Answers1

0

You need to initialize the RegionManager property before using it.

Example for your code:

protected override void InitializeShell()
{
    base.InitializeShell();
    App.Current.MainWindow = (Window)this.Shell;
    App.Current.MainWindow.Show();

    RegionManager = Prism.Regions.RegionManager.GetRegionManager(Application.Current.MainWindow);

    this.RegionManager.RequestNavigate("MainRegion", new Uri("PartNumberView", UriKind.Relative));
}
Florin Bratan
  • 155
  • 1
  • 6
  • Hi, thank you for your comment, it helped. But now it reutrns system.Object insted of the view itself. I shoul probably inform you that i dont use modules, but just view from mvvm model form project itself. Can you help me with that too ? I tried to use `Container.RegisterTypeForNavigation("PartNumberView");` but it did not help – Genna Oct 27 '19 at 07:07
  • `InitializeShell` is too early to begin with, rather go for the end of `InitializeModules`. There you should find a valid region manager _in the container_. – Haukinger Oct 27 '19 at 10:57