How does one implement pull-to-refresh with a LongListSelector in Windows Phone 8?

前端 未结 6 975
长情又很酷
长情又很酷 2021-02-11 06:09

I am writing a new WP8 app using the off-the-shelf LongListSelector that is shipped in the Microsoft.Phone.Controls assembly. Can anyone provide a code example that implements p

6条回答
  •  闹比i
    闹比i (楼主)
    2021-02-11 06:50

    This is not completely trivial, but one way of doing it is to use GestureService

            this.gestureListener = GestureService.GetGestureListener(containerPage);
            this.gestureListener.DragStarted += gestureListener_DragStarted;
            this.gestureListener.DragCompleted += gestureListener_DragCompleted;
            this.gestureListener.DragDelta += gestureListener_DragDelta;
    

    However, it has some bugs. For example, DragCompleted is not always raised, so you need to double-check for that using ManipulationCompleted event, which seems to be more reliable.

            containerPage.ManipulationStarted += delegate { this.manipulationInProgress = true; };
            containerPage.ManipulationCompleted += delegate
            { 
                this.manipulationInProgress = false;
                PerformDragComplete(); 
            };
    

    Another issue is that DragDelta occasionally reports bad coordinates. So you would need a fix like this:

        Point refPosition = e.GetPosition(null);
        if (refPosition.X == 0 && refPosition.Y == 0)
        {
            Tracer.WriteLine("Skipping buggy event");
            return;
        }
    

    Finally, you can find if list is all the way at the top:

    public double VerticalOffset
    {
        get
        {
            ViewportControl viewportControl = this.FindChildByName("ViewportControl") as ViewportControl;
            if (viewportControl != null)
            {
                Tracer.WriteLine("ViewPort.Bounds.Top=" + viewportControl.Bounds.Top +  " ViewPort.Top=" + viewportControl.Viewport.Top.ToString() + " State=" + this.ManipulationState);
                return viewportControl.Bounds.Top - viewportControl.Viewport.Top;
            }
            return double.NaN;
        }
    }
    

提交回复
热议问题