How to refresh by pulling down the items?

2019-06-13 02:56发布

问题:

there are lots of answers for this question, but, all of them as I see, are about getting old items at the bottom.

this solution:

void resultList_ItemRealized(object sender, ItemRealizationEventArgs e)
{
    if (!_viewModel.IsLoading && resultList.ItemsSource != null && resultList.ItemsSource.Count >= _offsetKnob)
    {
        if (e.ItemKind == LongListSelectorItemKind.Item)
        {
            if ((e.Container.Content as TwitterSearchResult).Equals(resultList.ItemsSource[resultList.ItemsSource.Count - _offsetKnob]))
            {
                _viewModel.LoadPage(_searchTerm, _pageNumber++);
            }
        }
    }
}

is about getting items as you scroll down.

How can I get fresh new content at top (not more old content at the bottom)? how can I know if user is scrolling up the LongListSelector

回答1:

Here are two posts for accomplishing this for the ListBox in Windows Phone 7 (which you have tagged) and will also work for Windows Phone 8

http://sharpgis.net/post/2011/04/03/RefreshBox-for-Windows-Phone-7.aspx http://blogs.msdn.com/b/jasongin/archive/2011/04/13/pull-down-to-refresh-a-wp7-listbox-or-scrollviewer.aspx

Both state to set

ScrollViewer.ManipulationMode ="Control"


回答2:

You can't rely on ItemRealized being fired. You need to add a listener on the ManipulationDelta event on LongListSelector. If the delta is negative the user scrolled up.

EDIT

I just remembered that Microsoft made some changed to LongListSelector (LLS) in Windows Phone 8 which means that it isn't possible to get in which direction the list is scrolling.

A workaround is to put the LLS in a ScrollViewer. Then set the Height property of the LLS to Auto. That way it is the ScrollViewer that scrolls and not the LLS. Then add a listener on the ScrollViewer's VerticalOffset.

private double _scrollingFrom;

ScrollViewer.Loaded += (sender, args) => VisualStateHelper.HookEvent<ScrollViewer>(ScrollViewer, "ScrollStates", scrollHandler);

private void scrollHandler(object sender, VisualStateChangedEventArgs e) {
    if (e.NewState.Name.Equals("NotScrolling")) {
        if (ScrollViewer.VerticalOffset < _scrollingFrom) {
            // Scrolled up
        } else {
            // Scrolled down
        }
    } else {
        _scrollingFrom = ScrollViewer.VerticalOffset;
    }
}

See here for VisualStateHelper