c#wpf-mvvm prism regionamanger属性在引导程序中为null



我试图创建一个新的WPF MVVM棱镜项目,但当我试图在shell中将视图添加到我的区域时,RegionManager属性出现了空引用异常。有人能帮我吗?这是我的代码:

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();
}

在使用RegionManager属性之前,您需要初始化它。

代码示例:

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));
}

最新更新