コード例 #1
0
 protected virtual void AddNamedViewIntoRegion(PreferredPositionNamedView viewToAdd)
 {
     if (Region is PreferredPositionRegion)
     {
         ((PreferredPositionRegion)Region).Add(viewToAdd, false);
     }
     else
     {
         Region.Add((viewToAdd.m_viewInstance ?? viewToAdd.m_viewInstanceProvider()), viewToAdd.m_viewName);
     }
 }
コード例 #2
0
        private void InnerAdd(PreferredPositionNamedView namedview, IRegionManager scopedRegionManager)
        {
            object view = (namedview.m_viewInstance ?? namedview.m_viewInstanceProvider());

            if (this.ItemMetadataCollection.FirstOrDefault(x => x.Item == view) != null)
            {
                throw new InvalidOperationException("RegionViewExistsException");
            }

            PreferredPositionItemMetadata itemMetadata = new PreferredPositionItemMetadata(view);

            if (!string.IsNullOrEmpty(namedview.m_viewName))
            {
                if (this.ItemMetadataCollection.FirstOrDefault(x => x.Name == namedview.m_viewName) != null)
                {
                    throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "RegionViewNameExistsException: {0}", namedview.m_viewName));
                }
                itemMetadata.Name = namedview.m_viewName;
            }

            itemMetadata.PreferredPosition = namedview.m_viewPreferredPosition;

            DependencyObject dependencyObject = view as DependencyObject;

            if (dependencyObject != null)
            {
                Microsoft.Practices.Composite.Presentation.Regions.RegionManager.SetRegionManager(dependencyObject, scopedRegionManager);
            }
#if true || DEBUG
            if (MARK_PREFERRED_POS)
            {
                markWithAddIndex(itemMetadata);
            }
#endif
            int initialCount = ItemMetadataCollection.Count;

            bool isSeparator = itemMetadata.Item is Separator
#if true || DEBUG
                               || MARK_PREFERRED_POS && !(itemMetadata.Item is MenuItemRichCommand) && itemMetadata.Item is MenuItem
#endif
            ;

            switch (itemMetadata.PreferredPosition)
            {
            case PreferredPosition.First:
            {
                var lastItemWithFirstPreferredPosition = ItemMetadataCollection.LastOrDefault(item =>
                    {
                        var positionedItem = item as PreferredPositionItemMetadata;
                        if (positionedItem == null)
                        {
                            Debug.Fail(@"This should never happen !! (not a PreferredPositionItemMetadata in PreferredPositionRegion)");
                            return(false);
                        }
                        return(positionedItem.PreferredPosition == PreferredPosition.First);
                    });

                // No First in the current list of views for this region
                if (lastItemWithFirstPreferredPosition == null)
                {
                    if (isSeparator)
                    {
                        break;                      // the following views will be inserted as the very first ones, no need for separation
                    }
                    if (ItemMetadataCollection.Count == 0)
                    {
                        ItemMetadataCollection.Add(itemMetadata);
#if true || DEBUG
                        if (MARK_PREFERRED_POS)
                        {
                            ItemMetadataCollectionCheckPreferredPositions();
                        }
#endif
                    }
                    else
                    {
                        //ItemMetadataCollection.Insert(0, itemMetadata);
                        //ItemMetadataCollectionInsert(0, itemMetadata);

                        var backup = new List <ItemMetadata>(initialCount);
                        while (ItemMetadataCollection.Count > 0)
                        {
                            var toRemove = ItemMetadataCollection.First();
                            ItemMetadataCollection.Remove(toRemove);
                            backup.Add(toRemove);
                        }

                        ItemMetadataCollection.Add(itemMetadata);
                        ItemMetadataCollection.AddRange(backup);

#if true || DEBUG
                        if (MARK_PREFERRED_POS)
                        {
                            ItemMetadataCollectionCheckPreferredPositions();
                        }
#endif
                    }

                    Debug.Assert(ItemMetadataCollection.Count == initialCount + 1);
                }
                else         // we found the last view with position == First
                {
                    int index = ItemMetadataCollection.IndexOf(lastItemWithFirstPreferredPosition);

                    // All the current views are First, so we append the new view (note: could be a Separator)
                    // i.e. the first view to be registered as First is guaranteed to take precedence over the ones that come after.
                    if (index == ItemMetadataCollection.Count - 1)
                    {
                        ItemMetadataCollection.Add(itemMetadata);

#if true || DEBUG
                        if (MARK_PREFERRED_POS)
                        {
                            ItemMetadataCollectionCheckPreferredPositions();
                        }
#endif
                    }
                    else         // there are Any or Last views after the last First one.
                    {
                        //ItemMetadataCollection.Insert(index + 1, itemMetadata);

                        ItemMetadataCollectionInsert(index + 1, itemMetadata);

#if true || DEBUG
                        if (MARK_PREFERRED_POS)
                        {
                            ItemMetadataCollectionCheckPreferredPositions();
                        }
#endif
                    }

                    Debug.Assert(ItemMetadataCollection.Count == initialCount + 1);
                }

                break;
            }

            case PreferredPosition.Last:
            {
                var lastItemWithLastPreferredPosition = ItemMetadataCollection.LastOrDefault(item =>
                    {
                        var positionedItem = item as PreferredPositionItemMetadata;
                        if (positionedItem == null)
                        {
                            Debug.Fail(@"This should never happen !! (not a PreferredPositionItemMetadata in PreferredPositionRegion)");
                            return(false);
                        }
                        return(positionedItem.PreferredPosition == PreferredPosition.Last);
                    });

                // No Last in the current list of views for this region
                if (lastItemWithLastPreferredPosition == null)
                {
                    ItemMetadataCollection.Add(itemMetadata);         // could be a separator

#if true || DEBUG
                    if (MARK_PREFERRED_POS)
                    {
                        ItemMetadataCollectionCheckPreferredPositions();
                    }
#endif
                }
                else         // we found the last view with position == Last
                {
                    int index = ItemMetadataCollection.IndexOf(lastItemWithLastPreferredPosition);
                    if (index == ItemMetadataCollection.Count - 1)
                    {
                        ItemMetadataCollection.Add(itemMetadata);         // could be a separator

#if true || DEBUG
                        if (MARK_PREFERRED_POS)
                        {
                            ItemMetadataCollectionCheckPreferredPositions();
                        }
#endif
                    }
                    else
                    {
                        Debug.Fail("No First or Any should follow a Last !!");

                        ////ItemMetadataCollection.Insert(index + 1, itemMetadata);
                        //ItemMetadataCollectionInsert(index + 1, itemMetadata);
                    }
                }

                Debug.Assert(ItemMetadataCollection.Count == initialCount + 1);

                break;
            }

            case PreferredPosition.Any:
            {
                var firstItemWithLastPreferredPosition = ItemMetadataCollection.FirstOrDefault(item =>
                    {
                        var positionedItem = item as PreferredPositionItemMetadata;
                        if (positionedItem == null)
                        {
                            Debug.Fail(@"This should never happen !! (not a PreferredPositionItemMetadata in PreferredPositionRegion)");
                            return(false);
                        }
                        return(positionedItem.PreferredPosition == PreferredPosition.Last);
                    });

                // No Last in the current list of views for this region
                if (firstItemWithLastPreferredPosition == null)
                {
                    ItemMetadataCollection.Add(itemMetadata);         // could be a separator

#if true || DEBUG
                    if (MARK_PREFERRED_POS)
                    {
                        ItemMetadataCollectionCheckPreferredPositions();
                    }
#endif
                }
                else
                {
                    int index = ItemMetadataCollection.IndexOf(firstItemWithLastPreferredPosition);

                    // ItemMetadataCollection.Insert(index, itemMetadata);
                    ItemMetadataCollectionInsert(index, itemMetadata);

#if true || DEBUG
                    if (MARK_PREFERRED_POS)
                    {
                        ItemMetadataCollectionCheckPreferredPositions();
                    }
#endif
                }

                break;
            }
            }
        }