在上文中还有不少bug,而且实现上拖泥带水,最近好好看了下scrollview的api,下面是精炼后的代码,少了很多,而且精准。
用的时候只需要在相应的事件里写入下面的代码就可以了。
第一种状态(提示下拉可以更新博客园新闻):
第二种状态(提示下拉的幅度已够可以释放进入更新操作)
第三种状态(提示正在更新,更新完毕回到第一个状态)
private double actuableOffset, validStartOffset;
private bool mplStarted;
protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
{
base.OnManipulationStarted(e);
mplStarted = true;
}
protected override void OnManipulationCompleted(ManipulationCompletedEventArgs e)
{
//150是下拉的距离可以自己调适
if (actuableOffset > 150 && scrollViewer.VerticalOffset == 0)
{
MessageBox.Show("Head pull bingo!");
}
if(actuableOffset<-150&&scrollViewer.VerticalOffset==scrollViewer.ScrollableHeight)
{
MessageBox.Show("Tail pull bingo!");
}
base.OnManipulationCompleted(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
//head capture startRelative
if ( scrollViewer.VerticalOffset == 0)
{
if (mplStarted)
{
mplStarted = false;
validStartOffset = e.GetPosition(null).Y;
}
actuableOffset = e.GetPosition(null).Y - validStartOffset;
}
//tail
if(scrollViewer.VerticalOffset==scrollViewer.ScrollableHeight)
{
if(mplStarted)
{
mplStarted = false;
validStartOffset = e.GetPosition(null).Y;
}
actuableOffset = e.GetPosition(null).Y - validStartOffset;
}
UIControlHelper.FindFirst<Pivot>().Title = actuableOffset;
base.OnMouseMove(e);
}
private bool mplStarted;
protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
{
base.OnManipulationStarted(e);
mplStarted = true;
}
protected override void OnManipulationCompleted(ManipulationCompletedEventArgs e)
{
//150是下拉的距离可以自己调适
if (actuableOffset > 150 && scrollViewer.VerticalOffset == 0)
{
MessageBox.Show("Head pull bingo!");
}
if(actuableOffset<-150&&scrollViewer.VerticalOffset==scrollViewer.ScrollableHeight)
{
MessageBox.Show("Tail pull bingo!");
}
base.OnManipulationCompleted(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
//head capture startRelative
if ( scrollViewer.VerticalOffset == 0)
{
if (mplStarted)
{
mplStarted = false;
validStartOffset = e.GetPosition(null).Y;
}
actuableOffset = e.GetPosition(null).Y - validStartOffset;
}
//tail
if(scrollViewer.VerticalOffset==scrollViewer.ScrollableHeight)
{
if(mplStarted)
{
mplStarted = false;
validStartOffset = e.GetPosition(null).Y;
}
actuableOffset = e.GetPosition(null).Y - validStartOffset;
}
UIControlHelper.FindFirst<Pivot>().Title = actuableOffset;
base.OnMouseMove(e);
}
本文转自today4king博客园博客,原文链接:http://www.cnblogs.com/jinzhao/archive/2012/02/27/2370424.html,如需转载请自行联系原作者