我在WP8上的LongListSelector中显示了一组图像,我使用LLS的ItemRealized事件实现了well known lazy loading pattern。
在下面的代码中,为图片集合中的每个项目调用OnItemRealized - 即使对于明显偏离屏幕的项目也是如此。在这种情况下,24个项目适合屏幕,但LLS实现了40个项目,这会触发ViewModel的ResumeGetPictures()。当图片集合发生变化(INotifyCollectionChanged)时,LLS也将实现这些项目,直到它用完项目,触发下一个ResumeGetPictures() - 这将持续到ViewModel无法加载更多项目。
只要LLS在LayoutMode = List中,所有似乎都很好。但是当我切换到Grid时,控件似乎吞下列表中的每个项目并立即实现它。任何类型的延迟加载都不可能。
我希望我做了一件非常非常错误的事情 - 虽然我怀疑这是因为我已经对所有内容进行了三重检查,就像我说切换到“列表”立即解决了问题 - 遗憾的是不能选择某种类型的照片库
视图模型:
public IReactiveDerivedList<TPicture> Pictures
{
get { return pictures; }
}
查看代码隐藏:
lls.ItemRealized += OnItemRealized;
private void OnItemRealized(object sender, ItemRealizationEventArgs e)
{
var picture = e.Container.Content as Picture;
if (picture != null)
{
// get index
var pictureIndex = lls.ItemsSource.IndexOf(picture);
if (pictureIndex >= lls.ItemsSource.Count * 0.95f)
ViewModel.ResumeGetPictures();
}
}
XAML:
<phone:LongListSelector Name="lls" Margin="13,-30,0,0"
ItemsSource="{Binding Pictures}"
Tap="OnListItemTapped"
ItemTemplate="{StaticResource ItemTemplate}"
IsGroupingEnabled="False"
LayoutMode="Grid"
GridCellSize="108,108"/>
答案 0 :(得分:1)
通过观察LLS内的ScrollBar,我能够获得所需的效果。我已将功能抽象为易于重复使用的行为:
public class LLSIncrementalLoadingBehavior : Behavior<LongListSelector>
{
private ScrollBar llsScrollBar;
#region Dependency Properties
public static readonly DependencyProperty RequestMoreDataProperty = DependencyProperty.Register(
"RequestMoreData", typeof(Action), typeof(LLSIncrementalLoadingBehavior), new PropertyMetadata(null, OnRequestMoreDataChanged));
/// <summary>
/// The action to invoke to initiate loading of more data
/// </summary>
public Action RequestMoreData
{
get { return (Action) this.GetValue(RequestMoreDataProperty); }
set { this.SetValue(RequestMoreDataProperty, value); }
}
private static void OnRequestMoreDataChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((LLSIncrementalLoadingBehavior)d).RequestMoreData = (Action)e.NewValue;
}
public static readonly DependencyProperty ThresholdProperty = DependencyProperty.Register(
"Threshold", typeof(double), typeof(LLSIncrementalLoadingBehavior), new PropertyMetadata(0.8, OnThresholdChanged));
/// <summary>
/// A value between 0 and 1 that controls how early more data is requested. Use 1 to only trigger it at the very end
/// </summary>
public double Threshold
{
get { return (double)this.GetValue(ThresholdProperty); }
set { this.SetValue(ThresholdProperty, value); }
}
private static void OnThresholdChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((LLSIncrementalLoadingBehavior)d).Threshold = (double)e.NewValue;
}
#endregion
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.Loaded += OnLoaded;
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
llsScrollBar = VisualTreeHelperExtensions.FindFirstElementInVisualTree<ScrollBar>(AssociatedObject);
llsScrollBar.ValueChanged += OnLlsScrollBarValueChanged;
}
private void OnLlsScrollBarValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
var bottomEdge = (float)(e.NewValue + AssociatedObject.ActualHeight);
var bottom = llsScrollBar.Maximum + AssociatedObject.ActualHeight;
var threshold = bottom * Threshold;
if (bottomEdge >= threshold)
RequestMoreData();
}
protected override void OnDetaching()
{
base.OnDetaching();
if (llsScrollBar != null)
{
llsScrollBar.ValueChanged -= OnLlsScrollBarValueChanged;
}
}
}
为了完整起见:
public static T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
{
if (parentElement != null)
{
var count = VisualTreeHelper.GetChildrenCount(parentElement);
if (count == 0)
return null;
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(parentElement, i);
if (child != null && child is T)
return (T)child;
else
{
var result = FindFirstElementInVisualTree<T>(child);
if (result != null)
{
return result;
}
}
}
}
return null;
}