【问题标题】:PagedCollectionView memory leak Silverlight 4PagedCollectionView 内存泄漏 Silverlight 4
【发布时间】:2010-08-24 13:47:20
【问题描述】:

我有一些项目的可观察集合。

 /// <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 因为我必须在数据网格中对项目进行分组

 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

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

 ItemGrouped = new PagedCollectionView(Items);
 ItemGrouped.GroupDescriptions.Add(new PropertyGroupDescription("GroupName"));

并且在视图中具有绑定 ItemsGrouped 的数据网格

<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>

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

【问题讨论】:

  • 您应该清除现有的收藏并再次将项目添加到收藏中,而不是每次都使用新的收藏。

标签: data-binding mvvm silverlight-4.0


【解决方案1】:

问题是从 RaisePropertyChanged(ItemsPropertyName); 连接到 NotifyPropertyChanged 的​​分页集合视图;并且永远不会释放事件挂钩......我解决了这个问题,因为我不需要通过返回 ICollectionView 进行分组。当您将网格绑定到 ObservableCollection 时,数据网格将为您创建 PagedCollectionView。当您绑定到 ICollectionView 时,网格将使用 ICollectionView 而无需创建 PagedCollectionView。希望这会有所帮助...

    public ICollectionView UserAdjustments
    {
        get { return _userAdjustmentsViewSource.View; }
    }

    private void SetCollection(List<UserAdjustment> adjustments)
    {
        if(_userAdjustmentsViewSource == null)
        {
            _userAdjustmentsViewSource = new CollectionViewSource();
        }
        _userAdjustmentsViewSource.Source = adjustments;
        RaisePropertyChanged("UserAdjustments");
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-25
    • 2011-09-12
    相关资源
    最近更新 更多