首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PagedCollectionView内存泄漏Silverlight 4

PagedCollectionView内存泄漏Silverlight 4
EN

Stack Overflow用户
提问于 2010-08-24 21:47:20
回答 1查看 1.9K关注 0票数 1

我有一些可观察的收藏品。

代码语言:javascript
复制
 /// <summary>
    /// The <see cref="Items" /> property's name.
    /// </summary>
    public const string ItemsPropertyName = "Items";

    private ObservableCollection<SomeItem> _items = new ObservableCollection<BrandItem>();

    /// <summary>
    /// Gets the Items property.
    /// </summary>
    public ObservableCollection<SomeItem> Items
    {
        get
        {

            return _items;
        }

        set
        {
            if (_items == value)
            {
                return;
            }

            _items = value;

            // Update bindings, no broadcast
            RaisePropertyChanged(ItemsPropertyName);
        }
    }

还有pagedCollectionView,因为我必须对数据网格中的项进行分组

代码语言:javascript
复制
 public const string ItemGroupedPropertyName = "ItemGrouped";

    private PagedCollectionView _itemsGrouped;

    /// <summary>
    /// Gets the ItemSpecificationsGrouped property.
    /// </summary>
    public PagedCollectionView ItemSpecificationsGrouped
    {
        get { return _itemsGrouped; }

        set
        {
            if (_itemsGrouped == value)
            {
                return;
            }

            _itemsGrouped = value;

            // Update bindings, no broadcast
            RaisePropertyChanged(ItemGroupedPropertyName);
        }
    }
    #endregion

在我设置的视图模型构造函数中

代码语言:javascript
复制
 ItemGrouped = new PagedCollectionView(Items);
 ItemGrouped.GroupDescriptions.Add(new PropertyGroupDescription("GroupName"));

并考虑到绑定ItemsGrouped have datagrid

代码语言:javascript
复制
<data:DataGrid ItemsSource="{Binding ItemsGrouped}" AutoGenerateColumns="False">
                                    <data:DataGrid.Columns >
                                        <data:DataGridTextColumn  IsReadOnly="True"
                                             Binding="{Binding ItemAttribut1}" Width="*"/>
                                        <data:DataGridTextColumn    IsReadOnly="True"
                                             Binding="{Binding Attribute2}" Width="*" />
                                    </data:DataGrid.Columns>

                            </data:DataGrid>

当我多次更改项中的项(清除并添加新项)时,会发生内存泄漏。当我删除ItemsSource时,一切都很好..所以我知道是PagedCollectionView导致了内存泄漏,但我不知道为什么。有什么想法吗?或者通过集合中的某些属性对datagrid中的项进行分组的另一种解决方案。谢谢你!!

EN

回答 1

Stack Overflow用户

发布于 2011-03-18 02:35:53

问题是分页集合视图挂起了来自RaisePropertyChanged的NotifyPropertyChanged (ItemsPropertyName);并且永远不会释放事件挂钩……我解决了这个问题,因为我不需要通过返回ICollectionView来进行分组。当您将网格绑定到ObservableCollection时,datagrid将为您创建PagedCollectionView。当您绑定到ICollectionView时,网格将使用ICollectionView而不创建PagedCollectionView。希望这能帮到你。

代码语言:javascript
复制
    public ICollectionView UserAdjustments
    {
        get { return _userAdjustmentsViewSource.View; }
    }

    private void SetCollection(List<UserAdjustment> adjustments)
    {
        if(_userAdjustmentsViewSource == null)
        {
            _userAdjustmentsViewSource = new CollectionViewSource();
        }
        _userAdjustmentsViewSource.Source = adjustments;
        RaisePropertyChanged("UserAdjustments");
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3557108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档