繁体   English   中英

ListView内存泄漏Windows Phone 8.1?

[英]ListView Memory Leak Windows Phone 8.1?

我有一个ListView绑定到对象的ObservableCollection(包含图像URI的对象),当我向ListView中添加更多项时,我看到内存大量增加。 我认为我已将其范围缩小为UserModel的imageUri问题。 见下文。

public class UserModel : ObservableObject
{
    ...
     private string _imageUri;
    ...


    ...
    public string ImageUri
    {
        get
        {
            return _imageUri;
        }
        set
        {
            Set(() => ImageUri, ref _imageUri, value);
        }
    } 
}

照片模型

public class PhotoModel : ObservableObject
{
    ...
    private UserModel _user;
    private string _imageUri;
    ...

    ...
    public UserModel User
    {
        get
        {
            return _user;
        }
        set
        {
            Set(() => User, ref _user, value);
        }
    }

    public string ImageUri
    {
        get
        {
            return _imageUri;
        }
        set
        {
            Set(() => ImageUri, ref _imageUri, value);
        }
    } 

}

Xaml绑定的ListView

<ListView
      x:Name="MostPopularListView"
      ItemsSource="{Binding PhotosCollection}"
      ItemTemplate="{StaticResource MostPopularDataTemplate}"
      Margin="0,0,0,0"
      IsItemClickEnabled="True"/>

Listview模板

       ...
       <Image 
          Source="{Binding ImageUri}"             
          Stretch="Fill" 
          Height="300" />

       ...

       <Ellipse 
            Width="40"
            Height="40" 
            Margin="10,0,0,10">
            <Ellipse.Fill>
                 <ImageBrush>
                     <ImageBrush.ImageSource>
                         <BitmapImage UriSource="{Binding User.ImageUri}" />
                            </ImageBrush.ImageSource>
                      </ImageBrush>
                 </Ellipse.Fill>
            </Ellipse>
            ...

如您所见,我的ListView数据模板有两张图像,一张用于实际照片,另一张用于用户。 两者都显示正确,但是当我继续向列表中添加更多项目时,我看到内存急剧增加。

见图片: 高内存配置文件

但是,如果我未设置UserModel.imageUri(UserModel.imageUri为null,则对于所有PhotoModels),我不会在内存中看到此峰值。 低内存配置文件

两个配置文件均执行相同的操作以加载相同的图像(共15个)。 第一张照片带有用户照片,第二张屏幕截图没有用户照片。

我认为问题与PhotoModel具有UserModel并执行Set(...)有关。 从下面的照片中可以看到,属性更改事件处理程序的计数为140。

资料1 在此处输入图片说明

其中大多数是PhotoModels,但我在收藏中最多只能有15个PhotoModels。 我确实使用两种扩展方法清除并重新添加了(可能是由它引起的)。

     public static void Repopulate<T>(this ICollection<T> collection, IEnumerable<T> items)
    {
        collection.Clear();
        foreach (var item in items)
        {
            collection.Add(item);
        }
    }

    public static void AddObjects<T>(this ICollection<T> collection, IEnumerable<T> items)
    {
        foreach (var item in items)
        {
            collection.Add(item);
        }
    }

我非常感谢您提供一些有关如何更好地处理性能以及是否存在内存泄漏的建议。

问题在于ImageBrush如何解码较大的图像。 您需要设置BitmapImageDecodePixelHeightDecodePixelWidth

图片来源: http : //timheuer.com/blog/archive/2015/05/06/making-circular-images-in-xaml-easily.aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM