Windows Phone 实现类似“微博”下拉刷新效果

简介:

在我的O7上跑了下张善友写的博客园阅读APP,有很多体验上的想法,一时技痒想实现下拉刷新的效果。

如果要实现下拉的效果就需要三个状态,见下图:

第一种状态(提示下拉可以更新博客园新闻): 

第二种状态(提示下拉的幅度已够可以释放进入更新操作) 

 

第三种状态(提示正在更新,更新完毕回到第一个状态)

 

上面这个效果为了方便独立到一个UserControl中,代码如下:

XAML:

复制代码
< UserControl  x:Class ="sdkRSSReaderCS.LoadingBox"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d
="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc
="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable
="d"
    FontFamily
=" {StaticResource PhoneFontFamilyNormal} "
    FontSize
=" {StaticResource PhoneFontSizeNormal} "
    Foreground
=" {StaticResource PhoneForegroundBrush} "
    d:DesignHeight
="480"  d:DesignWidth ="480" >

     < Grid  x:Name ="LayoutRoot" >
         < StackPanel  Orientation ="Horizontal" >
             < Grid  Margin ="8,0,0,0"  Width ="50"  Height ="50" >
                 < Image  x:Name ="PulldownArrow"  Width ="50"  Height ="50"  Source ="/sdkRSSReaderCS;component/images/pulldown_arrow.png" >
                     < Image.RenderTransform >
                         < RotateTransform  x:Name ="FlipTransform"  CenterX ="25"  CenterY ="25"  Angle ="180" />
                     </ Image.RenderTransform >
                     < Image.Resources >
                         < Storyboard  x:Name ="FlipDown" >
                             < DoubleAnimation  Storyboard.TargetName ="FlipTransform"
                               Storyboard.TargetProperty
="Angle"
                               From
="0"  To ="180"  Duration ="0:0:0.2" />
                         </ Storyboard >
                         < Storyboard  x:Name ="FlipUp" >
                             < DoubleAnimation  Storyboard.TargetName ="FlipTransform"
                               Storyboard.TargetProperty
="Angle"
                               From
="180"  To ="0"  Duration ="0:0:0.2" />
                         </ Storyboard >
                     </ Image.Resources >
                 </ Image >
                 < Image  x:Name ="SpinningBall"  Source ="/sdkRSSReaderCS;component/images/spinning_ball.png"  Width ="36"  Height ="36" >
                     < Image.RenderTransform >
                         < RotateTransform  x:Name ="SpinTransform"  CenterX ="18"  CenterY ="18" />
                     </ Image.RenderTransform >
                     < Image.Resources >
                         < Storyboard  x:Name ="Spin" >
                             < DoubleAnimation  Storyboard.TargetName ="SpinTransform"
                               Storyboard.TargetProperty
="Angle"
                               From
="0"  To ="360"  Duration ="0:0:1"
                               RepeatBehavior
="Forever" />
                         </ Storyboard >
                     </ Image.Resources >
                 </ Image >
             </ Grid >
             < TextBlock  x:Name ="TextBlock"  Margin ="10,0,0,0"
                 VerticalAlignment
="Center"  FontSize ="26"
                 Style
=" {StaticResource PhoneTextExtraLargeStyle} "
                 Foreground
=" {StaticResource PhoneAccentBrush} "   />
         </ StackPanel >
     </ Grid >
</ UserControl >
复制代码

Cs代码:

复制代码
     public  partial  class LoadingBox : UserControl
    {
         private  string arrow;
         public  static  readonly DependencyProperty BusyProperty = DependencyProperty.Register( " Busy "typeof( bool),  typeof(LoadingBox),  new PropertyMetadata( false));

         public LoadingBox()
        {
            InitializeComponent();
        }
         //  Properties
         public  string Arrow
        {
             get
            {
                 return  this.arrow;
            }
             set
            {
                 if ( this.arrow ==  null)
                {
                     if (value ==  " Invisible ")
                         this.PulldownArrow.Visibility = Visibility.Collapsed;
                     else  if (value ==  " Up ")
                    {
                         this.FlipTransform.Angle =  0.0;
                         this.PulldownArrow.Visibility = Visibility.Visible;
                    }
                     else  if (value ==  " Down ")
                    {
                         this.FlipTransform.Angle =  180.0;
                         this.PulldownArrow.Visibility = Visibility.Visible;
                    }
                }
                 else  if (value ==  " Invisible ")
                     this.PulldownArrow.Visibility = Visibility.Collapsed;
                 else  if (value ==  " Up ")
                {
                     if ( this.arrow ==  " Invisible ")
                    {
                         this.FlipTransform.Angle =  0.0;
                         this.PulldownArrow.Visibility = Visibility.Visible;
                    }
                     else  if ( this.arrow ==  " Down "this.FlipUp.Begin();
                }
                 else  if (value ==  " Down ")
                {
                     if ( this.arrow ==  " Invisible ")
                    {
                         this.FlipTransform.Angle =  180.0;
                         this.PulldownArrow.Visibility = Visibility.Visible;
                    }
                     else  if ( this.arrow ==  " Up "this.FlipDown.Begin();
                }
                 this.arrow = value;
            }
        }

         public  bool Busy
        {
             get
            {
                 return ( bool) base.GetValue(BusyProperty);
            }
             set
            {
                 base.SetValue(BusyProperty, value);
                 if (value)
                {
                     this.Arrow =  " Invisible ";
                     this.SpinningBall.Visibility = Visibility.Visible;
                     this.Spin.Begin();
                }
                 else
                {
                     this.SpinningBall.Visibility = Visibility.Collapsed;
                     this.Spin.Stop();
                }
            }
        }

         public  string Text
        {
             get
            {
                 return  this.TextBlock.Text;
            }
             set
            {
                 this.TextBlock.Text = value;
            }
        }

    }
复制代码

 下面是在引用的页面中的计算代码(里面的偏移量需要再具体引用的地方修改来适应相应的窗体和动作幅度):

复制代码
#region ScrollView

         private  void NewsScrollViewer_Loaded( object sender, RoutedEventArgs e)
        {
             this.newsScrollViewer = sender  as ScrollViewer;
        }


         private  void ScrollViewer_ManipulationCompleted( object sender, ManipulationCompletedEventArgs e)
        {
             this.actualOffset =  this.startOffset +  this.startPoint - e.ManipulationOrigin.Y;
             if ( this.actualOffset < - 100 && !headLoadingBox.Busy)
            {
                Update();
            }
             if ( this.actualOffset +  250 >  this.newsScrollViewer.ScrollableHeight && !tailLoadingBox.Busy)
            {
                NextPage();
            }

        }

         private  void ScrollViewer_ManipulationStarted( object sender, ManipulationStartedEventArgs e)
        {
             this.startOffset =  this.newsScrollViewer.VerticalOffset;
             this.startPoint = e.ManipulationOrigin.Y;
        }

         private  void ScrollViewer_MouseEnter( object sender, MouseEventArgs e)
        {
             // this.startRelativePoint = e.GetPosition(this.TopPlaceholder).Y;
        }

         private  void ScrollViewer_MouseMove( object sender, MouseEventArgs e)
        {
             this.actualOffset =  this.startOffset +  this.startRelativePoint - e.GetPosition( this.TopPlaceholder).Y;
             if (actualOffset < - 100)
            {
                 this.headLoadingBox.Arrow =  " Up ";
                 this.headLoadingBox.Text =  " 松开就可以更新! ";
            }
             if ( this.actualOffset +  250 >  this.newsScrollViewer.ScrollableHeight && !tailLoadingBox.Busy)
            {
                 this.tailLoadingBox.Arrow =  " Down ";
                 this.tailLoadingBox.Text =  " 松开就可以更新! ";
            }
        }
复制代码

 有不明白的问题请留言,需要代码的.



本文转自today4king博客园博客,原文链接:http://www.cnblogs.com/jinzhao/archive/2011/12/23/2299887.html,如需转载请自行联系原作者

相关文章
|
Java Windows
使用 windows bat 脚本命令一键实现快速配置JDK 环境变量
使用 windows bat 脚本命令一键实现快速配置JDK 环境变量
680 0
使用 windows bat 脚本命令一键实现快速配置JDK 环境变量
|
Java Windows
几步轻松实现可执行jar包在windows上直接启动
几步轻松实现可执行jar包在windows上直接启动
1878 0
|
消息中间件 安全 API
C#实现操作Windows窗口句柄:SendMessage/PostMessage发送系统消息、事件和数据【窗口句柄总结之二】
SendMessage/PostMessage API 可以实现发送系统消息,这些消息可以定义为常见的鼠标或键盘事件、数据的发送等各种系统操作......
5661 1
C#实现操作Windows窗口句柄:SendMessage/PostMessage发送系统消息、事件和数据【窗口句柄总结之二】
|
API C# Windows
C#实现操作Windows窗口句柄:常用窗口句柄相关API、Winform中句柄属性和Process的MainWindowHandle问题【窗口句柄总结之三】
本篇主要介绍一些与窗口句柄相关的一些API,比如设置窗口状态、当前激活的窗口、窗口客户区的大小、鼠标位置、禁用控件等,以及介绍Winform中的句柄属性,便于直接获取控件或窗体句柄,以及不推荐...
3194 0
C#实现操作Windows窗口句柄:常用窗口句柄相关API、Winform中句柄属性和Process的MainWindowHandle问题【窗口句柄总结之三】
|
Ubuntu 安全 Linux
【过关斩将般的一步步实现】windows本机通过xftp/xshell连接Ubuntu虚拟机服务器
【过关斩将般的一步步实现】windows本机通过xftp/xshell连接Ubuntu虚拟机服务器
1007 1
【过关斩将般的一步步实现】windows本机通过xftp/xshell连接Ubuntu虚拟机服务器
|
API C# Windows
C#实现操作Windows窗口句柄:遍历、查找窗体和控件【窗口句柄最全总结之一】
C#对Windows窗口或窗口句柄的操作,都是通过 P/Invoke Win32 API 实现的,DllImport引入Windows API操作窗口(句柄),可以实现枚举已打开的窗口、向窗口...
3798 0
C#实现操作Windows窗口句柄:遍历、查找窗体和控件【窗口句柄最全总结之一】
|
JSON Java API
C#winforms实现windows窗体人脸识别
C#winforms实现windows窗体人脸识别
339 0
|
Linux iOS开发 开发者
实现在windows、linux下上传ios app到App Store
实现在windows、linux下上传ios app到App Store
实现在windows、linux下上传ios app到App Store
|
JSON JavaScript 安全
基于Windows微信实现实时收发微信消息App
基于Windows微信实现实时收发微信消息App
1407 0
基于Windows微信实现实时收发微信消息App
|
Web App开发 前端开发 JavaScript
【我的前端】CSS在Windows下实现Mac浏览器滚动条
众所周知,Windows 和 macOS 浏览器的滚动条在默认情况下是不一致的,最为显著的是 macOS 的滚动条是不占据屏幕尺寸的,macOS 的滚动条如下:
【我的前端】CSS在Windows下实现Mac浏览器滚动条
下一篇
无影云桌面