自定义区域适配器 (PRISM)
Custom region adapter (PRISM)
我已经实现了工具栏的自定义区域适配器,如 link 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;
}
};
}
}
我已经覆盖了我的引导程序中的 ConfigureRegionAdapterMappings() 方法(我的引导程序继承自 MefBootstrapper)。这里的代码:
protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
{
RegionAdapterMappings regionAdapterMappings = base.ConfigureRegionAdapterMappings();
regionAdapterMappings.RegisterMapping(typeof(ToolBar), new ToolBarRegionAdapter());
return regionAdapterMappings;
}
编译时出现此错误:'ToolBarRegionAdapter' 不包含采用 0 个参数的构造函数。这实际上是真的,构造函数采用 IRegionBehaviorFactory 但我的代码中没有该对象。但是在我看到的示例中,区域适配器是在没有任何参数的情况下实例化的。
知道为什么吗?谢谢!
虽然构造函数注入始终是首选,但在不可能时(如您的情况),请使用服务定位器...
ServiceLocator.Current.GetInstance<IRegionBehaviorFactory >()
...如您提供的 link 所示,顺便说一句...
您添加适配器的方式有误:
必须是
protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
{
RegionAdapterMappings regionAdapterMappings = base.ConfigureRegionAdapterMappings();
regionAdapterMappings.RegisterMapping(typeof(ToolBar), Container.Resolve<ToolBarRegionAdapter>());
return regionAdapterMappings;
}
我已经实现了工具栏的自定义区域适配器,如 link 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;
}
};
}
}
我已经覆盖了我的引导程序中的 ConfigureRegionAdapterMappings() 方法(我的引导程序继承自 MefBootstrapper)。这里的代码:
protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
{
RegionAdapterMappings regionAdapterMappings = base.ConfigureRegionAdapterMappings();
regionAdapterMappings.RegisterMapping(typeof(ToolBar), new ToolBarRegionAdapter());
return regionAdapterMappings;
}
编译时出现此错误:'ToolBarRegionAdapter' 不包含采用 0 个参数的构造函数。这实际上是真的,构造函数采用 IRegionBehaviorFactory 但我的代码中没有该对象。但是在我看到的示例中,区域适配器是在没有任何参数的情况下实例化的。 知道为什么吗?谢谢!
虽然构造函数注入始终是首选,但在不可能时(如您的情况),请使用服务定位器...
ServiceLocator.Current.GetInstance<IRegionBehaviorFactory >()
...如您提供的 link 所示,顺便说一句...
您添加适配器的方式有误:
必须是
protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
{
RegionAdapterMappings regionAdapterMappings = base.ConfigureRegionAdapterMappings();
regionAdapterMappings.RegisterMapping(typeof(ToolBar), Container.Resolve<ToolBarRegionAdapter>());
return regionAdapterMappings;
}