WP7 工具包更新从 LongListSelector 中删除了 GetItemsInView()

发布于 2024-11-30 14:17:34 字数 334 浏览 1 评论 0原文

通过 Windows Phone Toolkit 的最新更新,他们彻底修改了 Mango 版本的 LongListSelector 的内部结构。其中一项更改是删除对 GetItemsInView() 函数的支持(它现在返回一个空列表)。该函数之前返回当前在屏幕上可见的项目列表。当离开页面时,我使用它来获取对最顶层可见项目的引用,以便我可以使用 ScrollTo(object item) 支持逻辑删除后的恢复。

有谁知道建议的替代方案是什么?我知道对于 Mango 墓碑来说问题要小得多,但我仍然想支持它,并且可能在其他一些情况下我想回忆滚动位置。在某些情况下,我的清单有数千个项目。

With the latest update to the Windows Phone Toolkit they have overhauled the internals of the LongListSelector for the Mango release. One of the changes was removing support for the GetItemsInView() function (it now returns an empty list). This function previously returned a list of items that were currently visible on the screen. I was using this to get a reference to the topmost visible item when navigating away from a page so that I could support recovery after a tombstone by using ScrollTo(object item).

Does anyone know what the suggested alternative would be? I know that with Mango tombstoning is much less of an issue, but I'd still like to support it and there may be some other scenarios where I'd want to recall the scroll position. My list has thousands of items in some cases.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风苍溪 2024-12-07 14:17:34

据我从新的位来看,您必须订阅 LLS 的 LinkUnlink 事件。 Link 将传入一个参数,其中包含添加到 LLS 可见部分的项目。 Unlink 对于从 LLS 中删除的项目执行相同的操作。因此,您需要执行以下操作:

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());
}

请注意,LinkUnlink 将为基础列表中的每个渲染项目触发,因此,如果您使用的是LLS,那么您必须根据实际返回的类型来增强是否跟踪该项目的测试。因此,如果您有某种组对象想要跟踪其底层对象,您可能会执行以下操作:

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);
        }
    }
}

我希望这会有所帮助!如果成功请告诉我们。

From what I can tell from the new bits, you have to subscribe to the LLS's Link and Unlink events. Link will pass in an arg that contains the item added to the visible part of the LLS. Unlink does the same for those items removed from the LLS. So you'd 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());
}

Note that Link and Unlink will fire for EVERY rendered item in the underlying list, so if you're using the grouping features of the LLS, then you'll have to augment your test on whether or not to track the item based on what type is actually coming back. So if you have some sort of group object for which you want to track the underrlying objects, you might 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);
        }
    }
}

I hope this helps! Let us know if it works out.

厌味 2024-12-07 14:17:34

LongListSelector 在内部使用 ScrollViewer(显然自 2011 年 8 月发布以来)。这一事实可用于按照 http://damianblog.com/2011/01/21/wp7-scroll-pivot/ 用于枢轴控制器。

OnNavieratedFrom() 中记住滚动偏移量:

    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;
    }

如果应用程序已被逻辑删除,则在 OnNavieratedTo() 中恢复它:

    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;
    }

The LongListSelector is using a ScrollViewer internally (apparently since the Aug 2011 release). This fact can be used to restore the position of the list after tombstoning by following the example given at http://damianblog.com/2011/01/21/wp7-scroll-pivot/ for the pivot 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 in OnNavigatedTo() if the app have been tombstoned:

    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;
    }
失去的东西太少 2024-12-07 14:17:34

链接/取消链接方法对于恢复滚动位置根本不起作用。即使您设置了集合,您也不知道是向上还是向下滚动,并且集合的大小会根据 LongListSelectorBufferSize 属性而变化。 。

然而,kvakulo 的答案中的 FindScrollViewer 解决方案是有效的。

如果有人需要此代码的 VB.Net 版本:

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

The Link/Unlink approach does not work at all for restoring the scroll position. Even if you setup the collection, you don't know if you are scrolling up or down, and the size of the collection would vary depending on the BufferSize property of the LongListSelector.

The FindScrollViewer solution in kvakulo's answer, however, works.

If someone needs the VB.Net version of this 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文