我對DI /棱鏡等非常陌生,並試圖教我自己。我從AutofacBootstrapper派生出來,它是Prism 6的附加組件。外殼初始化很好,但是在理解模塊註冊時遇到了問題。區域管理器顯示沒有區域
public class MyBootstrapper : AutofacBootstrapper
{
protected override DependencyObject CreateShell()
{
return Container.Resolve<Shell>();
}
protected override void InitializeShell()
{
base.InitializeShell();
Application.Current.MainWindow = (Window) Shell;
Application.Current.MainWindow.Show();
}
protected override void InitializeModules()
{
base.InitializeModules();
var builder = new ContainerBuilder();
var regionManager = new RegionManager();
builder.RegisterInstance(regionManager).As<IRegionManager>();
builder.Register(c => new ModuleAModule(c.Resolve<IRegionManager>()));
var container = builder.Build();
using (var scope = container.BeginLifetimeScope())
{
var component = scope.Resolve<ModuleAModule>();
component.Initialize();
}
}
}
public class ModuleAModule : IModule
{
private IRegionManager _regionManager;
private IContainer _container;
public ModuleAModule(IRegionManager regionManager)
{
_regionManager = regionManager;
}
public void Initialize()
{
_regionManager.RegisterViewWithRegion(RegionNames.ToolbarRegion, typeof(ToolbarView));
_regionManager.RegisterViewWithRegion(RegionNames.ContentRegion, typeof(ContentView));
}
}
外殼上來罰款2個區域,但是在初始化結束我檢查_regionManager.Regions.Count並得到0,而不是2,因爲我將創造2個區域。 xaml在下面。
<Window x:Class="WpfApplication1.Shell"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://www.codeplex.com/prism"
xmlns:wpfApplication1="clr-namespace:WpfApplication1"
Title="Shell">
<DockPanel LastChildFill="True">
<ContentControl DockPanel.Dock ="Top" prism:RegionManager.RegionName="{x:Static wpfApplication1:RegionNames.ToolbarRegion}"/>
<ContentControl prism:RegionManager.RegionName="{x:Static wpfApplication1:RegionNames.ContentRegion}"/>
</DockPanel>
</Window>
RegionNames簡單...
public static class RegionNames
{
public static readonly string ToolbarRegion = "ToolbarRegion";
public static readonly string ContentRegion = "ContentRegion";
}
所以我的問題是,爲什麼會RegionManager就沒有地區?
我覺得視覺樹的問題,看看http://stackoverflow.com/questions/33607639/region-not-loaded-by-the-regionmanger – toumir