Silverlight - Add/Remove view - ArgumentException

May 6, 2011 at 7:30 PM

Hi,

I dynamically add & remove a view to a region "B" when a main view in region "A" is navigated from (simplified example):

		public void OnNavigatedTo(NavigationContext navigationContext)
		{
			RegionManager.Regions["B"].Add(View);
		}
 
		public void OnNavigatedFrom(NavigationContext navigationContext)
		{
			RegionManager.Regions["B"].Remove(View);
		}

The particularity is that the View implements IRegionMemberLifetime and returns false for its KeepAlive property:

		public bool KeepAlive
		{
			get { return false; }
		}
 
When Remove is called, I get an ArgumentException. I debugged the Prism Library and what happens is that Prism tries to remove the view twice. Once when Remove is called from OnNavigatedFrom and one other time by Microsoft.Practices.Prism.Regions.Behaviors.RegionMemberLifetimeBehavior (culpright is the ShouldKeepAlive() call):
 
        private void OnActiveViewsChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            // We only pay attention to items removed from the ActiveViews list.
            // Thus, we expect that any ICollectionView implementation would
            // always raise a remove and we don't handle any resets
            // unless we wanted to start tracking views that used to be active.
            if (e.Action != NotifyCollectionChangedAction.Remove) return;
 
            var inactiveViews = e.OldItems;
            foreach (var inactiveView in inactiveViews)
            {
                if (!ShouldKeepAlive(inactiveView))
                {
                    this.Region.Remove(inactiveView);
                }
            }
        }
Is that behavior a bug? 
Thanks
May 9, 2011 at 9:11 PM

Hi,

We’ve reproduced the scenario that you’ve mentioned and found an issue in the Prism Library. We’ve created an issue in the Issue Tracker so the team can be notified about this behavior.

One possible workaround to avoid this issue from affecting your scenario would be to call the Region.Deactivate method instead of the Region.Remove method to remove the view from your region; since you’re setting KeepAlive to false, this should also remove the view from your region, but without causing this exception.

We have created a work item in the Issue Tracker to notify the team about this behavior

Thanks,

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