I'm new to Prism and following the tutorial Introduction to prism by Brian Lagunas, And I'm creating a custom regionAdapter like below.
public class StackPanelRegionAdapter : RegionAdapterBase<StackPanel>
{
public StackPanelRegionAdapter(IRegionBehaviorFactory regionBehavior )
:base(regionBehavior)
{
}
protected override void Adapt(IRegion region, StackPanel regionTarget)
{
region.Views.CollectionChanged += (s, e) =>
{
if(e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
{
foreach(FrameworkElement frameworkElement in e.NewItems)
{
regionTarget.Children.Add(frameworkElement);
}
}
};
}
protected override IRegion CreateRegion()
{
return new AllActiveRegion();
}
}
protected override void ConfigureContainer()
{
RegionAdapterMappings regionAdapterMappings = base.ConfigureRegionAdapterMappings();
regionAdapterMappings.RegisterMapping(typeof(StackPanel),
Container.Resolve<StackPanelRegionAdapter>());
}
I'm getting the exception
System.InvalidOperationException: ServiceLocationProvider must be set
at here
RegionAdapterMappings regionAdapterMappings = base.ConfigureRegionAdapterMappings();
What I'm doing wrong?
You should configure your region adapter mappings in an override of ConfigureRegionAdapterMappings
, not in an override of ConfigureContainer
that masquerades as that.
protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
{
var regionAdapterMappings = base.ConfigureRegionAdapterMappings();
regionAdapterMappings.RegisterMapping(typeof(StackPanel), ServiceLocator.Current.GetInstance<StackPanelRegionAdapter>());
return regionAdapterMappings;
}
Hint: you get the exception, because ConfigureContainer
is called before ConfigureServiceLocator
and base.ConfigureRegionAdapterMappings
uses ServiceLocator.Current
.