MEF WPF Add Ribbon to Region

Topics: Prism v4 - WPF 4
Dec 16, 2010 at 4:54 AM

SEE CODE BELOW

I am new to Prisim/MEF.  I am trying to add Rimmon toolbar to a region but am getting the following error:

An exception occurred while trying to create region objects.
    - The most likely causing exception was: 'System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Microsoft.Practices.Prism.Regions.Behaviors.RegionCreationException: An exception occurred while creating a region with name 'RibbonToolBarRegion'. The exception was: System.Collections.Generic.KeyNotFoundException: controlType
   at Microsoft.Practices.Prism.Regions.RegionAdapterMappings.GetMapping(Type controlType)
   at Microsoft.Practices.Prism.Regions.Behaviors.DelayedRegionCreationBehavior.CreateRegion(DependencyObject targetElement, String regionName).  ---> System.Collections.Generic.KeyNotFoundException: controlType
   at Microsoft.Practices.Prism.Regions.RegionAdapterMappings.GetMapping(Type controlType)
   at Microsoft.Practices.Prism.Regions.Behaviors.DelayedRegionCreationBehavior.CreateRegion(DependencyObject targetElement, String regionName)
   --- End of inner exception stack trace ---
   at Microsoft.Practices.Prism.Regions.Behaviors.DelayedRegionCreationBehavior.CreateRegion(DependencyObject targetElement, String regionName)
   at Microsoft.Practices.Prism.Regions.Behaviors.DelayedRegionCreationBehavior.TryCreateRegion()
   at Microsoft.Practices.Prism.Regions.Behaviors.DelayedRegionCreationBehavior.OnUpdatingRegions(Object sender, EventArgs e)
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Delegate.DynamicInvokeImpl(Object[] args)
   at Microsoft.Practices.Prism.Events.WeakDelegatesManager.Raise(Object[] args)
   at Microsoft.Practices.Prism.Regions.RegionManager.UpdateRegions()'.
    But also check the InnerExceptions for more detail or call .GetRootException().

My Bootstrapper is as follows:

public partial class Bootstrapper : MefBootstrapper
{
    protected override void ConfigureAggregateCatalog()
    {
        base.ConfigureAggregateCatalog();

        this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Shell).Assembly));
        this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(CoreModule).Assembly));

    }  // END: protected override void ConfigureAggregateCatalog()

    protected override void ConfigureContainer()
    {
        base.ConfigureContainer();

    }  // END: protected override void ConfigureContainer()

    protected override Microsoft.Practices.Prism.Modularity.IModuleCatalog CreateModuleCatalog()
    {
        return new ModuleCatalog().AddModule(typeof(CoreModule));

    }  // END: protected override Microsoft.Practices.Prism.Modularity.IModuleCatalog CreateModuleCatalog()

    protected override DependencyObject CreateShell()
    {
        return this.Container.GetExportedValue<Shell>();

    }  // END: protected override DependencyObject CreateShell()

    protected override void InitializeShell()
    {
        base.InitializeShell();

        IRegionManager regionManager = this.Container.GetExportedValue<IRegionManager>();
        regionManager.AddToRegion("RibbonToolBarRegion"typeof(XamRibbon));

        Application.Current.MainWindow = (Shell)this.Shell;
        Application.Current.MainWindow.Show();

    }  // END: protected override void InitializeShell() 

    protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
    {
        RegionAdapterMappings mappings = base.ConfigureRegionAdapterMappings();
        //mappings.RegisterMapping(typeof(XamRibbon), Container.GetExportedValue<XamRibbonRegionAdapter>());
        return mappings;

    }  // END: protected override RegionAdapterMappings ConfigureRegionAdapterMappings()  

}  // END: public partial class Bootstrapper : MefBootstrapper

My Shell is as follows:
<!-- RibbonToolBarRegion -->
        <Grid Margin="4,0,4,4" Grid.Column="0" Grid.Row="0" Grid.RowSpan="1">
            <Border Background="GhostWhite" BorderBrush="LightGray" 
                    BorderThickness="1" CornerRadius="5" Margin="0,0,0,0">
                <igRibbon:XamRibbon Name="xamShellRibbon" cal:RegionManager.RegionName="{x:Static inf:RegionNames.RibbonToolBarRegion}" 
                                    Height="100" Margin="0,0,0,0" VerticalAlignment="Top">
                    <igRibbon:XamRibbon.ApplicationMenu>
                        <igRibbon:ApplicationMenu Image="/RetailBeerSystems;component/Resources/Images/Orb.png" />
                    </igRibbon:XamRibbon.ApplicationMenu>
                    <igRibbon:XamRibbon.QuickAccessToolbar>
                        <igRibbon:QuickAccessToolbar />
                    </igRibbon:XamRibbon.QuickAccessToolbar>
                    <igRibbon:RibbonTabItem Header="Header 1">
                        <igRibbon:RibbonGroup>
                            <igRibbon:ButtonTool Content="Edit" />
                            <igRibbon:ButtonTool Content="Save" />
                        </igRibbon:RibbonGroup>
                    </igRibbon:RibbonTabItem>
                </igRibbon:XamRibbon>
            </Border>                
        </Grid>
My XamRibbonRegionAdapter is as follows:
[Export(typeof(XamRibbonRegionAdapter))]
public class XamRibbonRegionAdapter : RegionAdapterBase<XamRibbon>
{
    private XamRibbon xamRibbonRegionTarget = null;

    [ImportingConstructor]
    public XamRibbonRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
        : base(regionBehaviorFactory)
    {

    }

    protected override void Adapt(IRegion region, XamRibbon regionTarget)
    {
        // Do studd here
    }

    protected override IRegion CreateRegion()
    {
        return new AllActiveRegion();
    }

}

Any suggestions would or direction would be appreciated
Dec 16, 2010 at 3:33 PM
Edited Dec 16, 2010 at 3:35 PM

I think the direction you might want to try is making the regionadapter for the stuff that goes into the ribbon.  Think of the ribbon itself as a items control and you add items to it.  Therefore the regionadapter that you made, makes prism angry due to its only understanding contentcontrols, itemscontrols, and selectorcontrols.

You might consider making and adapter for the RibbonTab.  I am betting if you do a search for that control "infragistics ribbon"? that you will get a hit on someone else's adapter.  Also I see you have commented out the mapping for your adapter.

Dec 16, 2010 at 3:53 PM

Hi,

Based on my understanding of your scenario, the exception occurred because your xamRibbon region cannot be created. This might be caused, as Morgan suggested, by the fact that your xamRibbonRegionAdapter is not registered in the Boostsrapper´s RegionAdapterMapping collection (due to the line in the ConfigureRegionAdapterMappings method being commented).

To add to Morgan's comment, you might find the following threads useful to find a way to implement a xamRibbon RegionAdapter:

I hope you find this information useful.

Thanks,

Miguel Bronzovic
http://blogs.southworks.net/mbronzovic