我已经为工具栏实现了一个自定义区域适配器,如此链接 http://compositewpf.codeplex.com/discussions/250892 中所述.我收到此错误:“ToolBarRegionAdapter”不包含采用 0 个参数的构造函数。
这是我的代码:
public class ToolBarRegionAdapter : RegionAdapterBase<ToolBar>
{
public ToolBarRegionAdapter(IRegionBehaviorFactory behaviorFactory)
: base(behaviorFactory)
{
}
protected override IRegion CreateRegion()
{
return new AllActiveRegion();
}
protected override void Adapt(IRegion region, ToolBar regionTarget)
{
region.Views.CollectionChanged += (sender, e) =>
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
foreach (FrameworkElement element in e.NewItems)
{
regionTarget.Items.Add(element);
}
break;
case NotifyCollectionChangedAction.Remove:
foreach (UIElement elementLoopVariable in e.OldItems)
{
var element = elementLoopVariable;
if (regionTarget.Items.Contains(element))
{
regionTarget.Items.Remove(element);
}
}
break;
}
};
}
}
我已经在我的 Bootstrap 中覆盖了 ConfigureRegionAdapterMappings() 方法(我的 Bootstrap 继承自 MefBootstrapper)。这里的代码:
protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
{
RegionAdapterMappings regionAdapterMappings = base.ConfigureRegionAdapterMappings();
regionAdapterMappings.RegisterMapping(typeof(ToolBar), new ToolBarRegionAdapter());
return regionAdapterMappings;
}
编译时出现此错误:“ToolBarRegionAdapter”不包含采用 0 个参数的构造函数。这实际上是真的,构造函数采用 IRegionBehaviorFactory 但我的代码中没有该对象。但是在我看到的示例中,区域适配器是在没有任何参数的情况下实例化的。
知道为什么吗?谢谢!
最佳答案
虽然始终首选构造函数注入(inject),但当不可能时,如您的情况,请使用服务定位器...
ServiceLocator.Current.GetInstance<IRegionBehaviorFactory >()
...如您提供的链接所示,顺便说一句...
关于c# - 自定义区域适配器 (PRISM),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40578399/