(已编辑:我现在使用的是Unity而不是MEF,但仍然是同一个问题)
我为ToolBarTray区域实现了自定义区域适配器。我正在使用Unity的Prism框架。代码如下:
[Export]
public class ToolBarTrayRegionAdapter : RegionAdapterBase<ToolBarTray>
{
[ImportingConstructor]
public ToolBarTrayRegionAdapter(IRegionBehaviorFactory behaviorFactory)
: base(behaviorFactory)
{
}
protected override IRegion CreateRegion()
{
return new SingleActiveRegion();
}
protected override void Adapt(IRegion region, ToolBarTray regionTarget)
{
if (regionTarget.ToolBars.Count != 0)
throw new InvalidOperationException("Region target has toolbars");
region.ActiveViews.CollectionChanged += delegate
{
regionTarget.ToolBars.Add(region.ActiveViews.FirstOrDefault() as ToolBar);
};
}
}
我在bootstrapper overriden方法中添加了自己的自定义区域适配器映射:
protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
{
RegionAdapterMappings mappings = base.ConfigureRegionAdapterMappings();
mappings.RegisterMapping(typeof(ToolBar), Container.Resolve<ToolBarRegionAdapter>());
mappings.RegisterMapping(typeof(ToolBarTray), Container.Resolve<ToolBarTrayRegionAdapter>());
return mappings;
}
我的配置页面有一个左侧工具栏,其中包含用于不同模块的导航项,每个模块在ConfigurationContentRegion中注册一个ModuleView,在ConfigurationModulesToolBarTrayRegion中注册一个ToolBarView。这里是配置页面的xaml代码:
<Grid x:Name="LayoutConfiguration">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ToolBarTray DockPanel.Dock="Left" IsLocked="True" Orientation="Vertical" Style="{StaticResource SecondaryToolBarTrayStyle}">
<ToolBar x:Name="NavigationItemsControl" prism:RegionManager.RegionName="ConfigurationNavigationRegion" Style="{StaticResource SecondaryToolBarStyle}"/>
</ToolBarTray>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ToolBarTray prism:RegionManager.RegionName="ConfigurationModulesToolBarTrayRegion" DockPanel.Dock="Top" IsLocked="True" Orientation="Horizontal">
</ToolBarTray>
<ContentControl prism:RegionManager.RegionName="ConfigurationContentRegion"
Grid.Row="1" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"/>
</Grid>
</Grid>
</Grid>
我想要完成的是:每当按下导航项时,相应的模块视图将加载到ConfigurationContentRegion中,工具栏视图将加载到ConfigurationModulesToolBarTrayRegion中。
它编译得很好,但是当我按下导航项时会抛出NullReferenceException并且我得到一个&#34; ServiceLocatorImplBase.cs not found&#34;信息。有谁知道我在这里做错了什么?
这是我的模块工具栏视图代码隐藏代码:
[Export("CategoriesToolbarView")]
public partial class CategoriesToolbarView : UserControl
{
public CategoriesToolbarView()
{
InitializeComponent();
}
}
单击导航项时,此事件会将视图加载到区域中:
private void NavigateToCategoriesRadioButton_Click(object sender, RoutedEventArgs e)
{
this.regionManager.RequestNavigate(RegionNames.ConfigurationContentRegion, categoriesViewUri);
this.regionManager.RequestNavigate(RegionNames.ConfigurationModulesToolBarTrayRegion, categoriesToolbarViewUri);
}