拉刷新在Windows Phone [英] Pull to refresh On Windows Phone

查看:98
本文介绍了拉刷新在Windows Phone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一拉,刷新我的Windows Phone应用程序,

I am trying to implement a pull to refresh on my windows phone application,

我试过这个示例:的http:// code .msdn.microsoft.com / wpapps / TwitterSearch-Windows的b7fc4e5e
但是当我要编译的项目,我有这样的错误:

I tried this sample : http://code.msdn.microsoft.com/wpapps/TwitterSearch-Windows-b7fc4e5e but when I want to compile the project I have this error :

TwitterViewModel没有命名空间中存在CLR的命名空间:TwitterSample.ViewModels

TwitterViewModel does not exist in the namespace "clr-namespace:TwitterSample.ViewModels"

但组件在XAML正确引用:

But the assembly is properly referenced in the xaml :

xmlns:vm="clr-namespace:TwitterSample.ViewModels"

我怎样才能解决这个?

How can I correct this?

推荐答案

看看这个: WP8PullToRefreshDetector.cs

using Microsoft.Phone.Controls;
using System;
using System.Windows.Controls.Primitives;

/// <summary>
/// This class detects the pull gesture on a LongListSelector. How does it work?
/// 
///     This class listens to the change of manipulation state of the LLS, to the MouseMove event 
///     (in WP, this event is triggered when the user moves the finger through the screen)
///     and to the ItemRealized/Unrealized events.
///     
///     Listening to MouseMove, we can calculate the amount of finger movement. That is, we can 
///     detect when the user has scrolled the list.
///     
///     Then, when the ManipulationState changes from Manipulating to Animating (from user 
///     triggered movement to inertia movement), we check the viewport changes. The viewport is 
///     only constant when the user scrolls beyond the end of the list, either at the top or at the bottom.
///     If no items were added, check the direction of the scroll movement and fire the corresponding event.
/// </summary>
public class WP8PullDetector
{
    LongListSelector listbox;

    bool viewportChanged = false;
    bool isMoving = false;
    double manipulationStart = 0;
    double manipulationEnd = 0;

    public bool Bound { get; private set; }

    public void Bind(LongListSelector listbox)
    {
        Bound = true;
        this.listbox = listbox;
        listbox.ManipulationStateChanged += listbox_ManipulationStateChanged;
        listbox.MouseMove += listbox_MouseMove;
        listbox.ItemRealized += OnViewportChanged;
        listbox.ItemUnrealized += OnViewportChanged;
    }

    public void Unbind()
    {
        Bound = false;

        if (listbox != null)
        {
            listbox.ManipulationStateChanged -= listbox_ManipulationStateChanged;
            listbox.MouseMove -= listbox_MouseMove;
            listbox.ItemRealized -= OnViewportChanged;
            listbox.ItemUnrealized -= OnViewportChanged;
        }
    }

    void OnViewportChanged(object sender, Microsoft.Phone.Controls.ItemRealizationEventArgs e)
    {
        viewportChanged = true;
    }

    void listbox_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
    {
        var pos = e.GetPosition(null);

        if (!isMoving)
            manipulationStart = pos.Y;
        else
            manipulationEnd = pos.Y;

        isMoving = true;
    }

    void listbox_ManipulationStateChanged(object sender, EventArgs e)
    {
        if (listbox.ManipulationState == ManipulationState.Idle)
        {
            isMoving = false;
            viewportChanged = false;
        }
        else if (listbox.ManipulationState == ManipulationState.Manipulating)
        {
            viewportChanged = false;
        }
        else if (listbox.ManipulationState == ManipulationState.Animating)
        {
            var total = manipulationStart - manipulationEnd;

            if (!viewportChanged && Compression != null)
            {
                if (total < 0)
                    Compression(this, new CompressionEventArgs(CompressionType.Top));
                else if(total > 0) // Explicitly exclude total == 0 case
                    Compression(this, new CompressionEventArgs(CompressionType.Bottom));
            }
        }
    }


    public event OnCompression Compression;
}

public class CompressionEventArgs : EventArgs
{
    public CompressionType Type { get; protected set; }

    public CompressionEventArgs(CompressionType type)
    {
        Type = type;
    }
}

public enum CompressionType { Top, Bottom, Left, Right };

public delegate void OnCompression(object sender, CompressionEventArgs e);

用法:

public Page1()
{
    InitializeComponent();
    var objWP8PullDetector = new WP8PullDetector();
    objWP8PullDetector.Bind(objLongListSelector);
    //objWP8PullDetector.Unbind(); To unbind from compression detection
    objWP8PullDetector.Compression += objWP8PullDetector_Compression;
}

void objWP8PullDetector_Compression(object sender, CompressionEventArgs e)
{
    //TODO: Your logic here
}

这篇关于拉刷新在Windows Phone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆