WP7 Toolkit Update Removed GetItemsInView () from LongListSelector

With the latest update to the Windows Phone Toolkit, they have implemented the LongListSelector internal functions for the release of Mango. One of the changes was the removal of support for the GetItemsInView() function (now it returns an empty list). This function previously returned a list of items that were currently visible on the screen. I used this to get a link to the topmost visible element when navigating from the page, so that I can support recovery from the tombstone using ScrollTo(object item) .

Does anyone know what the proposed alternative has to offer? I know that mounting a Mango is much less of a problem, but I would still like to support it, and there may be other scenarios where I would like to recall the scroll position. In some cases, there are thousands of items on my list.

+6
source share
3 answers

From what I can say for the new bits, you should subscribe to the LLS Link and Unlink . Link is passed to arg, which contains the element added to the visible part of LLS. Unlink does the same for items that are removed from LLS. So you would do something like this:

 List<string> trackedItems = new List<string>(); private void myListOfStrings_Link(object sender, LinkUnlinkEventArgs e) { var x = e.ContentPresenter; if (x == null || x.Content == null) return; trackedItems.Add(x.Content.ToString()); } private void myListOfString_Unlink(object sender, LinkUnlinkEventArgs e) { var x = e.ContentPresenter; if (x == null || x.Content == null) return; trackedItems.Remove(x.Content.ToString()); } 

Please note that Link and Unlink will be launched for EVERY rendered item in the base list, so if you use the LLS grouping functions, you will have to increase your test of tracking the item based on what type is actually returned. Therefore, if you have some kind of group object for which you want to track the underlying objects, you can do something like this:

 private void myGroupedListOfObjects_Link(object sender, LinkUnlinkEventArgs e) { var x = e.ContentPresenter; if (x == null || x.Content == null) return; var myObject = x.Content as MyObject; if (myObject != null) { foreach (var item in myObject.Items) { trackedItems.Add(item); } } } 

Hope this helps! Let us know if this works.

+4
source

LongListSelector uses ScrollViewer internally (apparently since the August 2011 release). This fact can be used to restore the list position after the tombstone, following the example of http://damianblog.com/2011/01/21/wp7-scroll-pivot/ for the master controller.

In OnNavigatedFrom() remember the scroll offset:

  private bool _newPageInstance = true; private double _scollOffset = double.NaN; protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e) { base.OnNavigatedFrom(e); var scrollViewer = FindScrollViewer(LongList); State["scrollViewer.VerticalOffset"] = scrollViewer.VerticalOffset; State["PreservingPageState"] = true; _newPageInstance = false; } private static ScrollViewer FindScrollViewer(DependencyObject parent) { var childCount = VisualTreeHelper.GetChildrenCount(parent); for (var i = 0; i < childCount; i++) { var elt = VisualTreeHelper.GetChild(parent, i); if (elt is ScrollViewer) return (ScrollViewer)elt; var result = FindScrollViewer(elt); if (result != null) return result; } return null; } 

And restore it to OnNavigatedTo() if the application was buried:

  protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { base.OnNavigatedTo(e); if (!_newPageInstance || !State.ContainsKey("PreservingPageState")) { return; } _scollOffset = (double)State["scrollViewer.VerticalOffset"]; } private void LongList_Loaded(object sender, RoutedEventArgs e) { if (double.IsNaN(_scollOffset)) return; var longListSelector = (LongListSelector)sender; var scrollViewer = FindScrollViewer(longListSelector); scrollViewer.ScrollToVerticalOffset(_scollOffset); _scollOffset = double.NaN; } 
+3
source

The Link / Unlink approach does not work at all to restore the scroll position. Even if you are setting up a collection, you do not know if you are scrolling up or down, and the size of the collection will change depending on the BufferSize LongListSelector .

The FindScrollViewer solution in kvakulo responds however works.

If someone needs a version of this VB.Net code:

 Friend Function FindScrollViewer(parent As DependencyObject) As ScrollViewer Dim childCount = VisualTreeHelper.GetChildrenCount(parent) For i As Int32 = 0 To childCount - 1 Dim elt = VisualTreeHelper.GetChild(parent, i) If elt.GetType Is GetType(ScrollViewer) Then Return CType(elt, ScrollViewer) Dim result = FindScrollViewer(elt) If result IsNot Nothing Then Return result Next Return Nothing End Function 
+2
source

Source: https://habr.com/ru/post/895428/


All Articles