在Windows Phone 8中滚动到底部时,将更多项目加载到LonListSelector中

时间:2016-01-27 09:20:38

标签: c# xaml windows-phone-8

我是Windows手机开发的新手,我尝试在Windows Phone 8中滚动到底部时将更多项目加载到LonListSelector。

我使用此代码但scrollViewer会抛出null值异常。

ScrollViewer scrollViewer;
private void Mylist_Loaded(object sender, RoutedEventArgs e)
{
    //get the ScrollViewer from the ListBox
     scrollViewer = GetScrollViewer(this.MyLongListSelector);
    //attach to custom binding to check if ScrollViewer verticalOffset property has changed
    var binding = new Binding("VerticalOffset") { Source = scrollViewer };
    var offsetChangeListener = DependencyProperty.RegisterAttached(
        "ListenerOffset",
        typeof(object),
        typeof(UserControl),
        new PropertyMetadata(OnScrollChanged));
    scrollViewer.SetBinding(offsetChangeListener, binding);
}
// method to pull out a ScrollViewer
public static ScrollViewer GetScrollViewer(DependencyObject depObj)
{
    if (depObj is ScrollViewer) return depObj as ScrollViewer;

    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
    {
        var child = VisualTreeHelper.GetChild(depObj, i);

        var result = GetScrollViewer(child);
        if (result != null) return result;
    }
    return null;
}
public void OnScrollChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
    //We have to check if the values are 0.0 because they are both set to this when the scrollviewer loads
    if ((scrollViewer.ScrollableHeight <= scrollViewer.VerticalOffset)
        && (scrollViewer.ScrollableHeight != 0.0 && scrollViewer.VerticalOffset != 0.0))
    {
        //The ScrollBar is at the bottom, load more results.

    }
}

请帮帮我。

0 个答案:

没有答案