我有一个列表(简单的ListBox)的项目与图像的主细节基础(如果用户点击列表项目,详细信息页打开)。我面临着著名的图像内存泄漏问题,描述了here、here、here和here。
一种可能的方法是当run through all images NavigatingFrom and clean them时。
在one of the threads中,我发现了更有趣的解决方案:它自动清除图像,但这不适用于虚拟化(如果要添加用于存储ImageSource的私有字段,则图像丢失或混合)。建议的修复方法是添加依赖项属性。
但是我仍然面临着同样的问题:图片在向下滚动和返回后被混淆了。看起来依赖项属性是随机变化的,但我无法捕捉到它们变化的时刻。
public class SafePicture : ContentControl
{
public static readonly DependencyProperty SafePathProperty =
DependencyProperty.RegisterAttached(
"SafePath",
typeof(string),
typeof(SafePicture),
new PropertyMetadata(OnSourceWithCustomRefererChanged));
public string SafePath
{
get { return (string)GetValue(SafePathProperty); }
set { SetValue(SafePathProperty, value); }
}
private static void OnSourceWithCustomRefererChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue == null) // New value here
return;
}
public SafePicture()
{
Content = new Image();
Loaded += OnLoaded;
Unloaded += OnUnloaded;
}
private void OnLoaded(object _sender, RoutedEventArgs _routedEventArgs)
{
var image = Content as Image;
if (image == null)
return;
var path = (string)GetValue(SafePathProperty); // Also, tried SafePath (debugger cant catch setter and getter calls), but same result.
image.Source = null;
{
var request = WebRequest.Create(path) as HttpWebRequest;
request.AllowReadStreamBuffering = true;
request.BeginGetResponse(result =>
{
try
{
Stream imageStream = request.EndGetResponse(result).GetResponseStream();
DispatcherHelper.CheckBeginInvokeOnUI(() =>
{
if (imageStream == null)
{
image.Source = new BitmapImage { UriSource = new Uri(path, UriKind.Relative) };
return;
}
var bitmapImage = new BitmapImage();
bitmapImage.CreateOptions = BitmapCreateOptions.BackgroundCreation;
bitmapImage.SetSource(imageStream);
image.Source = bitmapImage;
});
}
catch (WebException)
{
}
}, null);
}
}
private void OnUnloaded(object sender, RoutedEventArgs e)
{
var image = Content as Image;
if (image == null)
return;
var bitmapImage = image.Source as BitmapImage;
if (bitmapImage != null)
bitmapImage.UriSource = null;
image.Source = null;
}
}用法:
<wpExtensions:SafePicture SafePath="{Binding ImageUrl}"/>因此,乍一看,它工作得很好,但是如果要向下滚动并返回向上,图像就会随机改变。
编辑:在本例中,目前我只使用纯ListBox,不使用虚拟化(但在其他情况下需要使用)。
EDIT2:重现此问题的示例项目。我相信,它会在一段时间内包含解决方案:https://simca.codeplex.com/
发布于 2013-10-02 13:10:34
问题是,当使用虚拟化时,用于每个项的ui元素将被回收并重用于其他对象(因此包括图像对象),并且由于您在足够快的滚动速度时异步加载图像,因此您将在图像上设置一个位图,该位图已经被用于另一个项。
快速修复的方法是检查路径值是否仍然相同,如果没有,只需返回,因为图像已经被用于另一个对象:
...
var request = WebRequest.Create(path) as HttpWebRequest;
request.AllowReadStreamBuffering = true;
request.BeginGetResponse(result =>
{
try
{
Stream imageStream = request.EndGetResponse(result).GetResponseStream();
DispatcherHelper.CheckBeginInvokeOnUI(() =>
{
if (path!=SafePath){
//Item has been recycled
return;
}
....编辑:代码中有几个问题:-Switch RegisterAttached to Register,RegisterAttached用于附加属性,而不是OnSourceWithCustomRefererChanged中的普通依赖属性-call OnLoaded,因为在加载元素之后,更改的SafePath属性实际上会在onLoaded开始时发生-Add清除uri和源,以便在路径为空时清除图像。
下面是一个完整的工作代码:
public class SafeImage : ContentControl
{
private SynchronizationContext uiThread;
public static readonly DependencyProperty SafePathProperty =
DependencyProperty.Register("SafePath", typeof (string), typeof (SafeImage),
new PropertyMetadata(default(string), OnSourceWithCustomRefererChanged));
public string SafePath
{
get { return (string) GetValue(SafePathProperty); }
set { SetValue(SafePathProperty, value); }
}
private static void OnSourceWithCustomRefererChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
SafeImage safeImage = o as SafeImage;
safeImage.OnLoaded(null, null);
//OnLoaded(null, null);
if (e.NewValue == null)
return;
}
public SafeImage()
{
Content = new Image();
uiThread = SynchronizationContext.Current;
Loaded += OnLoaded;
Unloaded += OnUnloaded;
}
private void OnLoaded(object _sender, RoutedEventArgs _routedEventArgs)
{
var image = Content as Image;
if (image == null)
return;
var path = SafePath; //(string)GetValue(SafePathProperty);
//image.Source = new BitmapImage(new Uri(SafePath));
Debug.WriteLine(path);
var bitmapImage = image.Source as BitmapImage;
if (bitmapImage != null)
bitmapImage.UriSource = null;
image.Source = null;
if (String.IsNullOrEmpty(path))
{
//image.Source = new BitmapImage { UriSource = new Uri(Constants.RESOURCE_IMAGE_EMPTY_PRODUCT, UriKind.Relative) };
return;
}
// If local image, just load it (non-local images paths starts with "http")
if (path.StartsWith("/"))
{
image.Source = new BitmapImage { UriSource = new Uri(path, UriKind.Relative) };
return;
}
{
var request = WebRequest.Create(path) as HttpWebRequest;
request.AllowReadStreamBuffering = true;
request.BeginGetResponse(result =>
{
try
{
Stream imageStream = request.EndGetResponse(result).GetResponseStream();
uiThread.Post(_ =>
{
if (path != this.SafePath)
{
return;
}
if (imageStream == null)
{
image.Source = new BitmapImage { UriSource = new Uri(path, UriKind.Relative) };
return;
}
bitmapImage = new BitmapImage();
bitmapImage.CreateOptions = BitmapCreateOptions.BackgroundCreation;
bitmapImage.SetSource(imageStream);
image.Source = bitmapImage;
//imageCache.Add(path, bitmapImage);
}, null);
}
catch (WebException)
{
//uiThread.Post(_ =>
//{
// image.Source = new BitmapImage { UriSource = new Uri(Constants.RESOURCE_IMAGE_EMPTY_PRODUCT, UriKind.Relative) };
//}, null);
}
}, null);
}
}
private void OnUnloaded(object sender, RoutedEventArgs e)
{
var image = Content as Image;
if (image == null)
return;
var bitmapImage = image.Source as BitmapImage;
if (bitmapImage != null)
bitmapImage.UriSource = null;
image.Source = null;
}
}最后,windows默认使用虚拟化和回收( ItemPanel是一个VirtualisedStackPanel)。
https://stackoverflow.com/questions/19136160
复制相似问题