问题是,当使用虚拟化时,用于每个项目的 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;
}
....
编辑:
代码中存在几个问题:
-将RegisterAttached切换到Register,RegisterAttached用于附加属性而不是普通的依赖属性
- 在 OnSourceWithCustomRefererChanged 中调用 OnLoaded,因为 SafePath 属性更改实际上可能在元素加载后发生
-在onLoaded开始处添加清除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 Phone ListBox 默认使用虚拟化和回收(使用的 ItemPanel 是 Virtualized StackPanel)。