[英]Windows Phone 8.1 WinRT memory leak with ObservableCollection
[英]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。
其中大多數是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
如何解碼較大的圖像。 您需要設置BitmapImage
的DecodePixelHeight
和DecodePixelWidth
圖片來源: http : //timheuer.com/blog/archive/2015/05/06/making-circular-images-in-xaml-easily.aspx
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.