Windows phone 7 滚动到底部时,将更多项目加载到列表框中
我已将列表框绑定到列表。当滚动到列表底部时,我想加载更多的项目,以便显示一些指示加载的动画,并展开绑定列表。如果我理解正确,我可以使用ObservableCollection而不是List并扩展该集合。此外,我还可以将ItemPresenter包装到StackPanel中,底部是ItemPresenter和图像 但如何检测列表已滚动到底部并启动集合扩展?查看教程:Windows phone 7 滚动到底部时,将更多项目加载到列表框中,windows-phone-7,Windows Phone 7,我已将列表框绑定到列表。当滚动到列表底部时,我想加载更多的项目,以便显示一些指示加载的动画,并展开绑定列表。如果我理解正确,我可以使用ObservableCollection而不是List并扩展该集合。此外,我还可以将ItemPresenter包装到StackPanel中,底部是ItemPresenter和图像 但如何检测列表已滚动到底部并启动集合扩展?查看教程: 我发现,在我看来,这比其他示例更符合、更简单、更容易解释。我将使用以下代码来解决相同的问题。作为基础,我使用n,添加了一个de
public class ScrollViewMonitor
{
public static readonly DependencyProperty ReachBottomCommandProperty = DependencyProperty.RegisterAttached("ReachBottomCommand",
typeof(ICommand), typeof(ScrollViewMonitor), new PropertyMetadata(null, ReachBottomCommandChanged));
public static ICommand GetReachBottomCommand(DependencyObject dpObj)
{
return (ICommand)dpObj.GetValue(ReachBottomCommandProperty);
}
public static void SetReachBottomCommand(DependencyObject dpObj, ICommand command)
{
dpObj.SetValue(ReachBottomCommandProperty, command);
}
private static FrameworkElement _frmElemenet;
private static void ReachBottomCommandChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
_frmElemenet = obj as FrameworkElement;
if (_frmElemenet != null)
_frmElemenet.Loaded += frmElement_Loaded;
}
public static readonly DependencyProperty VerticalOffsetProperty = DependencyProperty.RegisterAttached("VerticalOffset",
typeof(double), typeof(ScrollViewMonitor), new PropertyMetadata(0.0, VerticalOffsetChanged));
private static void VerticalOffsetChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
ScrollViewer scrollView = (ScrollViewer)obj;
if (scrollView.VerticalOffset > scrollView.ScrollableHeight * 0.8)
{
ICommand command = GetReachBottomCommand(_frmElemenet);
if (command != null)
command.Execute(null);
}
}
private static void frmElement_Loaded(object sender, RoutedEventArgs e)
{
FrameworkElement frmElement = (FrameworkElement)sender;
frmElement.Loaded -= frmElement_Loaded;
var scrollView = FindChildOfType<ScrollViewer>(frmElement);
if (scrollView != null)
scrollView.SetBinding(VerticalOffsetProperty, new Binding("VerticalOffset")
{
Source = scrollView
});
}
private static T FindChildOfType<T>(DependencyObject obj) where T : class
{
Queue<DependencyObject> queue = new Queue<DependencyObject>();
queue.Enqueue(obj); //Adds an object to the end of the
while (queue.Count > 0)
{
DependencyObject current = queue.Dequeue(); //Removes and returns the object at the beginning of the Queue
for (int i = 0, count = VisualTreeHelper.GetChildrenCount(current); i < count; i++)
{
DependencyObject dpObj = VisualTreeHelper.GetChild(current, 0);
T typeChild = dpObj as T;
if (typeChild != null)
return typeChild;
queue.Enqueue(dpObj); //Adds an object to the end of the Queue
}
}
return null;
}
}
公共类ScrollViewMonitor
{
公共静态只读DependencyProperty ReachBottomCommandProperty=DependencyProperty.RegisterAttached(“ReachBottomCommand”,
typeof(ICommand)、typeof(ScrollViewMonitor)、新属性元数据(null,ReachBottomCommandChanged));
公共静态ICommand GetReachBottomCommand(DependencyObject dpObj)
{
返回(ICommand)dpObj.GetValue(ReachBottomCommandProperty);
}
公共静态无效SetReachBottomCommand(DependencyObject dpObj、ICommand命令)
{
设置值(ReachBottomCommandProperty,command);
}
私有静态框架元素_frmelement;
私有静态void ReachBottomCommandChanged(DependencyObject对象,DependencyPropertyChangedEventArgs参数)
{
_frmElemenet=作为框架元素的obj;
如果(_frmelement!=null)
_frmElement.Loaded+=frmElement_Loaded;
}
公共静态只读DependencyProperty VerticalOffsetProperty=DependencyProperty.RegisterAttached(“VerticalOffset”,
typeof(双精度)、typeof(ScrollViewMonitor)、新的PropertyMetadata(0.0,垂直偏移设置);
私有静态void VerticalOffsetChanged(DependencyObject对象、DependencyPropertyChangedEventArgs参数)
{
ScrollViewer scrollView=(ScrollViewer)obj;
如果(scrollView.VerticalOffset>scrollView.ScrollableHeight*0.8)
{
ICommand命令=GetReachBottomCommand(_frmelement);
if(命令!=null)
command.Execute(null);
}
}
已加载私有静态void frmElement_(对象发送方,路由目标)
{
FrameworkElement FRMELENT=(FrameworkElement)发送方;
FRMELENT.Loaded-=FRMELENT_Loaded;
var scrollView=FindChildOfType(frmElement);
if(滚动视图!=null)
scrollView.SetBinding(VerticalOffsetProperty,新绑定(“VerticalOffset”)
{
Source=滚动视图
});
}
私有静态T FindChildOfType(DependencyObject obj),其中T:class
{
队列=新队列();
queue.Enqueue(obj);//将对象添加到
而(queue.Count>0)
{
DependencyObject current=queue.Dequeue();//删除并返回队列开头的对象
for(int i=0,count=VisualTreeHelper.GetChildrenCount(当前);i
在XAML中
<ListBox x:Name="_lstBoxNews"
DataContext="{Binding Mode=OneTime}"
ItemsSource="{Binding Items, Mode=OneWay}"
ItemTemplate="{Binding Mode=OneWay, Source={StaticResource ShortArticleTemlate}}"
hlp:ScrollViewMonitor.ReachBottomCommand="{Binding LoadAdditionalArticles, Mode=OneTime}"
>
</ListBox>