RegionManager 显示没有 Region
RegionManager shows no Region
我对 DI / Prism 等还很陌生,正在尝试自学。我派生自 AutofacBootstrapper,它是 Prism 6 的附加组件。Shell 初始化很好,但在理解模块注册时遇到了问题。
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));
}
}
shell 在 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 没有区域?
对于 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 处设置一个断点,您会在该点看到现在视图已注入该区域。那是因为引导程序的初始化没有完成。您会在输出 window.
中看到
调试:正在初始化模块。优先级:低。 Timestamp:2015-12-17 09:58:09Z.
0 <-- Console.WriteLine(_regionManager.Regions.Count());
调试:引导程序序列已完成。优先级:低。 Timestamp:2015-12-17 09:58:09Z. <-- Boostrapper 完成:-)
如果您稍后在程序中询问,现在您将得到 2。
希望对您有所帮助 ;-)
我对 DI / Prism 等还很陌生,正在尝试自学。我派生自 AutofacBootstrapper,它是 Prism 6 的附加组件。Shell 初始化很好,但在理解模块注册时遇到了问题。
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));
}
}
shell 在 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 没有区域?
对于 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 处设置一个断点,您会在该点看到现在视图已注入该区域。那是因为引导程序的初始化没有完成。您会在输出 window.
中看到调试:正在初始化模块。优先级:低。 Timestamp:2015-12-17 09:58:09Z.
0 <-- Console.WriteLine(_regionManager.Regions.Count());
调试:引导程序序列已完成。优先级:低。 Timestamp:2015-12-17 09:58:09Z. <-- Boostrapper 完成:-)
如果您稍后在程序中询问,现在您将得到 2。
希望对您有所帮助 ;-)