0

I have implemented a custom region adapter for a ToolBar as explained in this link http://compositewpf.codeplex.com/discussions/250892. I get this error:'ToolBarRegionAdapter' does not contain a constructor that takes 0 arguments. Here my code:

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;
            }
        };
    }
}

I have overrided the ConfigureRegionAdapterMappings() method in my Bootstrapper (my Bootstrapper inherits from MefBootstrapper). Here the code:

protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
{
    RegionAdapterMappings regionAdapterMappings = base.ConfigureRegionAdapterMappings();
    regionAdapterMappings.RegisterMapping(typeof(ToolBar), new ToolBarRegionAdapter());
    return regionAdapterMappings;
}

When I compile I get this error:'ToolBarRegionAdapter' does not contain a constructor that takes 0 arguments. Which is actually true, the contructor takes a IRegionBehaviorFactory but I don't have that object in my code. But in the examples I've seen, the region adapter is instantiated without any argument. Any idea why? Thanks!

Jim
  • 2,974
  • 2
  • 19
  • 29
chincheta73
  • 187
  • 1
  • 22

2 Answers2

1

While constructor injection is always preferred, when it's not possible, as in your case, go for the service locator...

ServiceLocator.Current.GetInstance<IRegionBehaviorFactory >()

... as is shown in the link you provided, btw...

Haukinger
  • 10,420
  • 2
  • 15
  • 28
  • Thanks @Haukinger. It worked. However I don't understand some bits and peaces here (sorry, I'm quite new to Prism). **Question 1**: ServiceLocator is a "box" where you can get instances of what kind of objects? ; **Question 2**: What is "constructor injection"?; and **Question 3**: "Why constructor injection is not possible in my case? – chincheta73 Nov 13 '16 at 21:52
  • ad 1) kind-of, yes, prism configures the common service locator to access the container used, so it's the same as `Container.Resolve`. ad 2) constructor injection means having the dependencies as constructor parameters and the container filling them in when resolving. ad 3) constructor injection is only possible if the constructor is called by the container, that is, if the instance is produced by `Container.Resolve` – Haukinger Nov 14 '16 at 07:04
  • FYI, don't use the ServcieLocator, just use the container directly. http://brianlagunas.com/create-a-custom-prism-regionadapter/ –  Nov 14 '16 at 14:47
1

You are wrong with how you add adapter:

Must be

protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
{
    RegionAdapterMappings regionAdapterMappings = base.ConfigureRegionAdapterMappings();
    regionAdapterMappings.RegisterMapping(typeof(ToolBar), Container.Resolve<ToolBarRegionAdapter>());
    return regionAdapterMappings;
}
galakt
  • 1,374
  • 13
  • 22
  • If I add adapter that way I get this error: "CompositionContainer does not contain a definitio for 'Resolve' and no extension method 'Resolve' accepting a first argument of type CompositionContainer could be found"... – chincheta73 Nov 23 '16 at 08:46