Hi,
I'm creating a modular application where modules can add items to a ribbon control. I've created a region on a RibbonTabItem and I have modules which register RibbonGroup views to it. However an ArgumentOutOfRangeException sometomes occurs (but not always) when a second item is being added to the region. The exception is thrown in the region.ActiveViews.CollectionChanged event implementation when new items are being added and it gets to _RegionTarget.RibbonGroups.Add(view). Code and stack trace below. Any help you be appreciated.
RegionAdapter Code:
=========================================
public class RibbonTabRegionBehaviour : RegionAdapterBase<RibbonTabItem> { private RibbonTabItem _RegionTarget = null; public RibbonTabRegionBehaviour(IRegionBehaviorFactory regionBehaviorFactory) : base(regionBehaviorFactory) { } protected override void Adapt(IRegion region, RibbonTabItem regionTarget) { _RegionTarget = regionTarget; if (_RegionTarget.RibbonGroups.Any<RibbonGroup>()) { foreach (RibbonGroup ribbonGroup in _RegionTarget.RibbonGroups) { region.Add(ribbonGroup); } _RegionTarget.RibbonGroups.Clear(); } foreach (object view in region.Views) { AddView(view); } region.ActiveViews.CollectionChanged += new NotifyCollectionChangedEventHandler(ActiveViews_CollectionChanged); } private void ActiveViews_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { switch (e.Action) { case NotifyCollectionChangedAction.Add: foreach (object view in e.NewItems) { AddView(view); } break; case NotifyCollectionChangedAction.Remove: foreach (object current in e.OldItems) { if (!(current is RibbonGroup)) { throw new NotSupportedException(string.Format("View of type '{0}' is not supported.", current.GetType().Name)); } _RegionTarget.RibbonGroups.Remove(current as RibbonGroup); } break; } } protected override IRegion CreateRegion() { return new AllActiveRegion(); } private void AddView(object view) { RibbonGroup rti = view as RibbonGroup; if (rti != null) { if(!_RegionTarget.RibbonGroups.Contains(view)) _RegionTarget.RibbonGroups.Add(rti); } } }
==================================
Exception details:
<event errorType="ModuleInitializeException"> <message>An exception occurred while initializing module 'WebUIWebLink'. - The exception message was: An exception has occurred while trying to add a view to region 'HomeTabRegion'. - The most likely causing exception was was: 'System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.Parameter name: index at System.Windows.Media.VisualCollection.Insert(Int32 index, Visual visual) at System.Windows.Controls.Panel.AddChildren(GeneratorPosition pos, Int32 itemCount) at System.Windows.Controls.Panel.OnItemsChangedInternal(Object sender, ItemsChangedEventArgs args) at System.Windows.Controls.Panel.OnItemsChanged(Object sender, ItemsChangedEventArgs args) at System.Windows.Controls.ItemContainerGenerator.OnItemAdded(Object item, Int32 index) at System.Windows.Controls.ItemContainerGenerator.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) at System.Windows.WeakEventManager.ListenerList`1.DeliverEvent(Object sender, EventArgs e, Type managerType) at System.Windows.WeakEventManager.DeliverEvent(Object sender, EventArgs args) at System.Collections.Specialized.CollectionChangedEventManager.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e) at System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args) at System.Windows.Controls.ItemCollection.OnViewCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) at System.Windows.WeakEventManager.ListenerList`1.DeliverEvent(Object sender, EventArgs e, Type managerType) at System.Windows.WeakEventManager.DeliverEvent(Object sender, EventArgs args) at System.Collections.Specialized.CollectionChangedEventManager.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) at System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args) at System.Windows.Data.ListCollectionView.ProcessCollectionChangedWithAdjustedIndex(NotifyCollectionChangedEventArgs args, Int32 adjustedOldIndex, Int32 adjustedNewIndex) at System.Windows.Data.ListCollectionView.ProcessCollectionChanged(NotifyCollectionChangedEventArgs args) at System.Windows.Data.CollectionView.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e) at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e) at Infragistics.Collections.ObservableCollectionExtended`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e) at Infragistics.Collections.ObservableCollectionExtended`1.InsertItem(Int32 index, T item) at System.Collections.ObjectModel.Collection`1.Add(T item) at AgeasUK.AgentToolbar.Application.Core.Modularity.Infragistics.RibbonTabRegionBehaviour.ActiveViews_CollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e) at Microsoft.Practices.Prism.Regions.ViewsCollection.OnCollectionChanged(NotifyCollectionChangedEventArgs e) at Microsoft.Practices.Prism.Regions.ViewsCollection.NotifyAdd(IList items, Int32 newStartingIndex) at Microsoft.Practices.Prism.Regions.ViewsCollection.NotifyAdd(Object item) at Microsoft.Practices.Prism.Regions.ViewsCollection.SourceCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e) at System.Collections.ObjectModel.ObservableCollection`1.InsertItem(Int32 index, T item) at System.Collections.ObjectModel.Collection`1.Add(T item) at Microsoft.Practices.Prism.Regions.Region.InnerAdd(Object view, String viewName, IRegionManager scopedRegionManager) at Microsoft.Practices.Prism.Regions.Region.Add(Object view, String viewName, Boolean createRegionManagerScope) at Microsoft.Practices.Prism.Regions.Region.Add(Object view) at Microsoft.Practices.Prism.Regions.Behaviors.AutoPopulateRegionBehavior.AddViewIntoRegion(Object viewToAdd) at Microsoft.Practices.Prism.Regions.Behaviors.AutoPopulateRegionBehavior.OnViewRegistered(Object sender, ViewRegisteredEventArgs e)'. But also check the InnerExceptions for more detail. </message> <data /> <stackTrace> at Microsoft.Practices.Prism.Modularity.ModuleInitializer.HandleModuleInitializationError(ModuleInfo moduleInfo, String assemblyName, Exception exception) at Microsoft.Practices.Prism.Modularity.ModuleInitializer.Initialize(ModuleInfo moduleInfo) at Microsoft.Practices.Prism.Modularity.ModuleManager.LoadModulesThatAreReadyForLoad() at Microsoft.Practices.Prism.Modularity.ModuleManager.LoadModuleTypes(IEnumerable`1 moduleInfos) at Microsoft.Practices.Prism.Modularity.ModuleManager.LoadModule(String moduleName) at AgeasUK.AgentToolbar.Modules.Startup.UI.ViewModels.StartupViewModel.<>c__DisplayClass9.<LoadWorker_DoWork>b__4()</stackTrace> <source>Microsoft.Practices.Prism.Composition</source> <event errorType="ViewRegistrationException"> <message>An exception has occurred while trying to add a view to region 'HomeTabRegion'. - The most likely causing exception was was: 'System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.Parameter name: index at System.Windows.Media.VisualCollection.Insert(Int32 index, Visual visual) at System.Windows.Controls.Panel.AddChildren(GeneratorPosition pos, Int32 itemCount) at System.Windows.Controls.Panel.OnItemsChangedInternal(Object sender, ItemsChangedEventArgs args) at System.Windows.Controls.Panel.OnItemsChanged(Object sender, ItemsChangedEventArgs args) at System.Windows.Controls.ItemContainerGenerator.OnItemAdded(Object item, Int32 index) at System.Windows.Controls.ItemContainerGenerator.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) at System.Windows.WeakEventManager.ListenerList`1.DeliverEvent(Object sender, EventArgs e, Type managerType) at System.Windows.WeakEventManager.DeliverEvent(Object sender, EventArgs args) at System.Collections.Specialized.CollectionChangedEventManager.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e) at System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args) at System.Windows.Controls.ItemCollection.OnViewCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) at System.Windows.WeakEventManager.ListenerList`1.DeliverEvent(Object sender, EventArgs e, Type managerType) at System.Windows.WeakEventManager.DeliverEvent(Object sender, EventArgs args) at System.Collections.Specialized.CollectionChangedEventManager.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) at System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args) at System.Windows.Data.ListCollectionView.ProcessCollectionChangedWithAdjustedIndex(NotifyCollectionChangedEventArgs args, Int32 adjustedOldIndex, Int32 adjustedNewIndex) at System.Windows.Data.ListCollectionView.ProcessCollectionChanged(NotifyCollectionChangedEventArgs args) at System.Windows.Data.CollectionView.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e) at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e) at Infragistics.Collections.ObservableCollectionExtended`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e) at Infragistics.Collections.ObservableCollectionExtended`1.InsertItem(Int32 index, T item) at System.Collections.ObjectModel.Collection`1.Add(T item) at AgeasUK.AgentToolbar.Application.Core.Modularity.Infragistics.RibbonTabRegionBehaviour.ActiveViews_CollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e) at Microsoft.Practices.Prism.Regions.ViewsCollection.OnCollectionChanged(NotifyCollectionChangedEventArgs e) at Microsoft.Practices.Prism.Regions.ViewsCollection.NotifyAdd(IList items, Int32 newStartingIndex) at Microsoft.Practices.Prism.Regions.ViewsCollection.NotifyAdd(Object item) at Microsoft.Practices.Prism.Regions.ViewsCollection.SourceCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e) at System.Collections.ObjectModel.ObservableCollection`1.InsertItem(Int32 index, T item) at System.Collections.ObjectModel.Collection`1.Add(T item) at Microsoft.Practices.Prism.Regions.Region.InnerAdd(Object view, String viewName, IRegionManager scopedRegionManager) at Microsoft.Practices.Prism.Regions.Region.Add(Object view, String viewName, Boolean createRegionManagerScope) at Microsoft.Practices.Prism.Regions.Region.Add(Object view) at Microsoft.Practices.Prism.Regions.Behaviors.AutoPopulateRegionBehavior.AddViewIntoRegion(Object viewToAdd) at Microsoft.Practices.Prism.Regions.Behaviors.AutoPopulateRegionBehavior.OnViewRegistered(Object sender, ViewRegisteredEventArgs e)'. But also check the InnerExceptions for more detail or call .GetRootException(). </message> <data /> <stackTrace> at Microsoft.Practices.Prism.Regions.RegionViewRegistry.OnContentRegistered(ViewRegisteredEventArgs e) at Microsoft.Practices.Prism.Regions.RegionViewRegistry.RegisterViewWithRegion(String regionName, Func`1 getContentDelegate) at Microsoft.Practices.Prism.Regions.RegionViewRegistry.RegisterViewWithRegion(String regionName, Type viewType) at Microsoft.Practices.Prism.Regions.RegionManagerExtensions.RegisterViewWithRegion(IRegionManager regionManager, String regionName, Type viewType) at AgeasUK.AgentToolbar.Modules.Web.UI.WebLink.ModuleDefinition.Initialize() at Microsoft.Practices.Prism.Modularity.ModuleInitializer.Initialize(ModuleInfo moduleInfo)</stackTrace> <source>Microsoft.Practices.Prism.Composition</source> <event errorType="ArgumentOutOfRangeException"> <message>Specified argument was out of the range of valid values.Parameter name: index</message> <data /> <stackTrace> at System.Windows.Media.VisualCollection.Insert(Int32 index, Visual visual) at System.Windows.Controls.Panel.AddChildren(GeneratorPosition pos, Int32 itemCount) at System.Windows.Controls.Panel.OnItemsChangedInternal(Object sender, ItemsChangedEventArgs args) at System.Windows.Controls.Panel.OnItemsChanged(Object sender, ItemsChangedEventArgs args) at System.Windows.Controls.ItemContainerGenerator.OnItemAdded(Object item, Int32 index) at System.Windows.Controls.ItemContainerGenerator.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) at System.Windows.WeakEventManager.ListenerList`1.DeliverEvent(Object sender, EventArgs e, Type managerType) at System.Windows.WeakEventManager.DeliverEvent(Object sender, EventArgs args) at System.Collections.Specialized.CollectionChangedEventManager.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e) at System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args) at System.Windows.Controls.ItemCollection.OnViewCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) at System.Windows.WeakEventManager.ListenerList`1.DeliverEvent(Object sender, EventArgs e, Type managerType) at System.Windows.WeakEventManager.DeliverEvent(Object sender, EventArgs args) at System.Collections.Specialized.CollectionChangedEventManager.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) at System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args) at System.Windows.Data.ListCollectionView.ProcessCollectionChangedWithAdjustedIndex(NotifyCollectionChangedEventArgs args, Int32 adjustedOldIndex, Int32 adjustedNewIndex) at System.Windows.Data.ListCollectionView.ProcessCollectionChanged(NotifyCollectionChangedEventArgs args) at System.Windows.Data.CollectionView.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e) at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e) at Infragistics.Collections.ObservableCollectionExtended`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e) at Infragistics.Collections.ObservableCollectionExtended`1.InsertItem(Int32 index, T item) at System.Collections.ObjectModel.Collection`1.Add(T item) at AgeasUK.AgentToolbar.Application.Core.Modularity.Infragistics.RibbonTabRegionBehaviour.ActiveViews_CollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e) at Microsoft.Practices.Prism.Regions.ViewsCollection.OnCollectionChanged(NotifyCollectionChangedEventArgs e) at Microsoft.Practices.Prism.Regions.ViewsCollection.NotifyAdd(IList items, Int32 newStartingIndex) at Microsoft.Practices.Prism.Regions.ViewsCollection.NotifyAdd(Object item) at Microsoft.Practices.Prism.Regions.ViewsCollection.SourceCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e) at System.Collections.ObjectModel.ObservableCollection`1.InsertItem(Int32 index, T item) at System.Collections.ObjectModel.Collection`1.Add(T item) at Microsoft.Practices.Prism.Regions.Region.InnerAdd(Object view, String viewName, IRegionManager scopedRegionManager) at Microsoft.Practices.Prism.Regions.Region.Add(Object view, String viewName, Boolean createRegionManagerScope) at Microsoft.Practices.Prism.Regions.Region.Add(Object view) at Microsoft.Practices.Prism.Regions.Behaviors.AutoPopulateRegionBehavior.AddViewIntoRegion(Object viewToAdd) at Microsoft.Practices.Prism.Regions.Behaviors.AutoPopulateRegionBehavior.OnViewRegistered(Object sender, ViewRegisteredEventArgs e)</stackTrace> <source>PresentationCore</source> </event> </event> </event>
=============
Thanks In Advance,
Andy G
Hello Andy,
I am checking if this is still an issue for you.
If you require any further assistance please do not hesitate to ask.
Annoyingly I'm unable to reproduce the issue in a simpler example (with less than 20+ PRISM modules). However I have got a work-around on the RibbonTabRegionBehaviour code which appears to work (/ hide the error and everything appears to work fine after a refresh of the ribbon group collection):
try
{ _RegionTarget.RibbonGroups.Add(rti); } catch(ArgumentOutOfRangeException) { // this is a big cheat to get around a bug when an exception gets thrown here which doesn't make sense. The below fixes it. if (_RegionTarget.RibbonGroups.Contains(rti)) { var groups = _RegionTarget.RibbonGroups.ToList(); _RegionTarget.RibbonGroups.Clear(); _RegionTarget.RibbonGroups.AddRange(groups); } else { throw; }
}
Hi Andy,
Thank you for your reply. I am glad that you have managed to resolve your issue. I believe that other visitors of our community can get benefit from this.