RegionManager不显示Region

时间:2015-12-14 21:04:03

标签: prism autofac

我对DI / Prism等很新,并试图自学。我从AutofacBootstrapper派生出来,这是Prism 6的补充。壳初始化很好但是在理解模块注册时遇到了麻烦。

use strict;
use warnings;
use Net::SMTP 3.03;
my $mailer = Net::SMTP->new(
    'smtp.mail.yahoo.com',
    Hello   =>      'smtp.mail.yahoo.com',
    Port    =>      587,
);

$mailer->starttls();
$mailer->auth('someEmail@yahoo.com','password');
$mailer->mail(...)

shell有2个区域可以正常运行,但是在Initialize结束时,我正在检查_regionManager.Regions.Count并获得0而不是2,因为我创建了2个区域。 xaml在下面。

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

RegionNames很简单......

<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>

所以我的问题是为什么RegionManager没有区域?

1 个答案:

答案 0 :(得分:0)

对于Prism v6,你使用了错误的命名空间:

xmlns:prism="http://prismlibrary.com/"

然后它有效。

以下是我的ContentView代码:

<UserControl x:Class="WpfApplication3.ContentView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:WpfApplication3"
         mc:Ignorable="d">
    <Grid Background="Blue">

    </Grid>
</UserControl>

和工具栏视图:

<UserControl x:Class="WpfApplication3.ToolbarView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:WpfApplication3"
         mc:Ignorable="d" >
    <Grid Background="Aqua" Height="20">

    </Grid>
</UserControl>

编辑:

抱歉,我原谅了一部分。

public void Initialize()
{
    _regionManager.RegisterViewWithRegion(RegionNames.ToolbarRegion, typeof(ToolbarView));
    _regionManager.RegisterViewWithRegion(RegionNames.ContentRegion, typeof(ContentView));

    Console.WriteLine(_regionManager.Regions.Count());
}

如果在Console.WriteLine上设置断点,那么此时您将看到视图被注入到Region。那是因为没有完成bootstrapper的Initialize。你会在输出窗口看到它。

DEBUG:初始化模块..优先级:低。时间戳:2015-12-17 09:58:09Z。

0 &lt; - Console.WriteLine(_regionManager.Regions.Count());

DEBUG:Bootstrapper序列已完成..优先级:低。时间戳:2015-12-17 09:58:09Z。&lt; - Boostrapper完成: - )

如果您稍后在程序中询问,那么您将获得2。

我希望现在有所帮助; - )