我已经实现了工具栏的自定义区域适配器,如链接http://compositewpf.codeplex.com/discussions/250892中所述。我得到这个错误:'ToolBarRegionAdapter'不包含一个带0参数的构造函数。 这里我的代码:自定义区域适配器(PRISM)
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;
}
};
}
}
我在我的引导程序overrided的ConfigureRegionAdapterMappings()方法(我的引导程序从MefBootstrapper继承)。下面的代码:
protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
{
RegionAdapterMappings regionAdapterMappings = base.ConfigureRegionAdapterMappings();
regionAdapterMappings.RegisterMapping(typeof(ToolBar), new ToolBarRegionAdapter());
return regionAdapterMappings;
}
当我编译我得到这个错误:“ToolBarRegionAdapter”不包含一个构造函数参数0。这其实是真的,构造器需要一个IRegionBehaviorFactory,但我没有在我的代码中的那个对象。但在我看到的例子中,区域适配器没有任何参数就被实例化了。 任何想法为什么?谢谢!
感谢@Haukinger。有效。不过,我不明白这里的一些点点滴滴(对不起,我对棱镜颇为陌生)。 **问题1 **:ServiceLocator是一个“盒子”,您可以在其中获得什么样的对象的实例? ; **问题2 **:什么是“构造函数注入”?和**问题3 **:“为什么构造函数注入在我的情况下是不可能的? – chincheta73
ad 1)类,是的,棱镜配置通用服务定位器来访问使用的容器,所以它与Container.Resolve '。ad 2)构造函数注入意味着将依赖关系作为构造函数参数,并在解析时将容器填充它们ad 3)构造函数注入只有在容器调用构造函数时才可能,也就是说,如果实例是由' Container.Resolve' – Haukinger
仅供参考,请勿使用ServcieLocator,直接使用容器。http://brianlagunas.com/create-a-custom-prism-regionadapter/ –