uwp - 上滑隐藏导航栏下滑显示
好久没写博客了,因为忙着工作。昨天周末填坑需要做一个上滑列表数据时隐藏导航栏下滑时显示的效果,下面分享一下我的做法,希望能给你带来帮助。
思路是通过判断滚动条是往下还是往上滑然后做出相应的显示隐藏导航栏处理即可;
首先在我们的内容外套一个ScrollViewer,然后在ScrollViewer的事件ViewChanged中做判断。
我们新 建一个page,布局出列表(包含模拟测试用的数据)、导航栏,代码如下:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ScrollViewer x:Name="sv" ViewChanged="sv_ViewChanged">
<!--模拟数据-->
<StackPanel>
<Grid Height="50" Margin="0,10,0,0" >
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<LinearGradientBrush.RelativeTransform>
<CompositeTransform CenterY="0.5" CenterX="0.5" Rotation="-90"/>
</LinearGradientBrush.RelativeTransform>
<GradientStop Color="#FF3FBBD6" Offset="0"/>
<GradientStop Color="#FFF7AFED" Offset="1"/>
</LinearGradientBrush>
</Grid.Background>
<TextBlock Text="模拟数据1" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</Grid>
<Grid Height="50" Margin="0,10,0,0" >
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<LinearGradientBrush.RelativeTransform>
<CompositeTransform CenterY="0.5" CenterX="0.5" Rotation="-90"/>
</LinearGradientBrush.RelativeTransform>
<GradientStop Color="#FF3FBBD6" Offset="0"/>
<GradientStop Color="#FFF7AFED" Offset="1"/>
</LinearGradientBrush>
</Grid.Background>
<TextBlock Text="模拟数据2" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</Grid>
<Grid Height="50" Margin="0,10,0,0" >
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<LinearGradientBrush.RelativeTransform>
<CompositeTransform CenterY="0.5" CenterX="0.5" Rotation="-90"/>
</LinearGradientBrush.RelativeTransform>
<GradientStop Color="#FF3FBBD6" Offset="0"/>
<GradientStop Color="#FFF7AFED" Offset="1"/>
</LinearGradientBrush>
</Grid.Background>
<TextBlock Text="模拟数据3" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</Grid>
<Grid Height="50" Margin="0,10,0,0" >
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<LinearGradientBrush.RelativeTransform>
<CompositeTransform CenterY="0.5" CenterX="0.5" Rotation="-90"/>
</LinearGradientBrush.RelativeTransform>
<GradientStop Color="#FF3FBBD6" Offset="0"/>
<GradientStop Color="#FFF7AFED" Offset="1"/>
</LinearGradientBrush>
</Grid.Background>
<TextBlock Text="模拟数据4" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</Grid>
<Grid Height="50" Margin="0,10,0,0" >
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<LinearGradientBrush.RelativeTransform>
<CompositeTransform CenterY="0.5" CenterX="0.5" Rotation="-90"/>
</LinearGradientBrush.RelativeTransform>
<GradientStop Color="#FF3FBBD6" Offset="0"/>
<GradientStop Color="#FFF7AFED" Offset="1"/>
</LinearGradientBrush>
</Grid.Background>
<TextBlock Text="模拟数据5" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</Grid>
<Grid Height="50" Margin="0,10,0,0" >
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<LinearGradientBrush.RelativeTransform>
<CompositeTransform CenterY="0.5" CenterX="0.5" Rotation="-90"/>
</LinearGradientBrush.RelativeTransform>
<GradientStop Color="#FF3FBBD6" Offset="0"/>
<GradientStop Color="#FFF7AFED" Offset="1"/>
</LinearGradientBrush>
</Grid.Background>
<TextBlock Text="模拟数据6" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</Grid>
<Grid Height="50" Margin="0,10,0,0" >
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<LinearGradientBrush.RelativeTransform>
<CompositeTransform CenterY="0.5" CenterX="0.5" Rotation="-90"/>
</LinearGradientBrush.RelativeTransform>
<GradientStop Color="#FF3FBBD6" Offset="0"/>
<GradientStop Color="#FFF7AFED" Offset="1"/>
</LinearGradientBrush>
</Grid.Background>
<TextBlock Text="模拟数据7" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</Grid>
<Grid Height="50" Margin="0,10,0,0" >
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<LinearGradientBrush.RelativeTransform>
<CompositeTransform CenterY="0.5" CenterX="0.5" Rotation="-90"/>
</LinearGradientBrush.RelativeTransform>
<GradientStop Color="#FF3FBBD6" Offset="0"/>
<GradientStop Color="#FFF7AFED" Offset="1"/>
</LinearGradientBrush>
</Grid.Background>
<TextBlock Text="模拟数据8" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</Grid>
<Grid Height="50" Margin="0,10,0,0" >
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<LinearGradientBrush.RelativeTransform>
<CompositeTransform CenterY="0.5" CenterX="0.5" Rotation="-90"/>
</LinearGradientBrush.RelativeTransform>
<GradientStop Color="#FF3FBBD6" Offset="0"/>
<GradientStop Color="#FFF7AFED" Offset="1"/>
</LinearGradientBrush>
</Grid.Background>
<TextBlock Text="模拟数据9" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</Grid>
<Grid Height="50" Margin="0,10,0,0" >
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<LinearGradientBrush.RelativeTransform>
<CompositeTransform CenterY="0.5" CenterX="0.5" Rotation="-90"/>
</LinearGradientBrush.RelativeTransform>
<GradientStop Color="#FF3FBBD6" Offset="0"/>
<GradientStop Color="#FFF7AFED" Offset="1"/>
</LinearGradientBrush>
</Grid.Background>
<TextBlock Text="模拟数据10" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</Grid>
<Grid Height="50" Margin="0,10,0,0" >
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<LinearGradientBrush.RelativeTransform>
<CompositeTransform CenterY="0.5" CenterX="0.5" Rotation="-90"/>
</LinearGradientBrush.RelativeTransform>
<GradientStop Color="#FF3FBBD6" Offset="0"/>
<GradientStop Color="#FFF7AFED" Offset="1"/>
</LinearGradientBrush>
</Grid.Background>
<TextBlock Text="模拟数据11" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</Grid>
<Grid Height="50" Margin="0,10,0,0" >
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<LinearGradientBrush.RelativeTransform>
<CompositeTransform CenterY="0.5" CenterX="0.5" Rotation="-90"/>
</LinearGradientBrush.RelativeTransform>
<GradientStop Color="#FF3FBBD6" Offset="0"/>
<GradientStop Color="#FFF7AFED" Offset="1"/>
</LinearGradientBrush>
</Grid.Background>
<TextBlock Text="模拟数据12" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</Grid>
<Grid Height="50" Margin="0,10,0,0" >
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<LinearGradientBrush.RelativeTransform>
<CompositeTransform CenterY="0.5" CenterX="0.5" Rotation="-90"/>
</LinearGradientBrush.RelativeTransform>
<GradientStop Color="#FF3FBBD6" Offset="0"/>
<GradientStop Color="#FFF7AFED" Offset="1"/>
</LinearGradientBrush>
</Grid.Background>
<TextBlock Text="模拟数据13" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</Grid>
</StackPanel>
</ScrollViewer>
<!--导航栏-->
<Grid x:Name="bar" Height="50" Background="#FFB05AEC" VerticalAlignment="Bottom" Canvas.ZIndex="2" Opacity="0.9">
<TextBlock Text="导航栏" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White"></TextBlock>
</Grid>
</Grid>
MainPage.xaml
以上代码的效果是这样的:

然后转到mainpage的后台,代码如下:
//滚动条位置变量
double scrlocation = 0;
//导航栏当前显示状态(这个是为了减少不必要的开销,因为我做的是动画隐藏显示效果如果不用一个变量来记录当前导航栏状态的会重复执行隐藏或显示)
bool isshowbmbar = true;
public MainPage()
{
this.InitializeComponent(); } private void sv_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
{
if (sv.VerticalOffset != scrlocation)
{
if (sv.VerticalOffset > scrlocation)
{
//滚动条当前位置大于存储的变量值时代表往下滑,隐藏底部栏
//隐藏
if (isshowbmbar)
{
//这里为了简洁易懂就不做动画隐藏效果,直接用透明度进行隐藏。
bar.Opacity = 0;
isshowbmbar = false; }
}
else
{
//显示
if (isshowbmbar == false)
{
bar.Opacity = 1;
isshowbmbar = true; } } }
scrlocation = sv.VerticalOffset;
}
mainpage.xaml.cs
代码中也写了非常详细的注释了,这里在简单说几句,我用一个变量scrlocation来记录滚动停止后的滚动条所在位置,然后在列表滚动时也就是viewchanged事件发生时获取滚动条的位置,只要判断viewchanged事件发生时获取滚动条的位置是大于scrlocation还是小于就可以知道用户是往上滑还是往下滑了。这里还有一个变量isshowbmbar是记录当前导航栏的显示状态,因为滑动列表的时候viewchanged事件会不停地触发,如果不用这个变量记录并且判断的话会导致导航栏不停地显示或隐藏,如果你只是单纯用透明度来隐藏或显示的话性能影响上来说不算太大,但是如果你像我用动画来做隐藏显示效果的话会加大这个开销或者出现动画显示不正常的情况。
以上代码运行起来的效果如下(没有动画效果看起来不是很好看,懒得写懒啊大家自己加上就和我第一张效果图一样了。):

希望我的思路能给你带来帮助,谢谢。如果写得不好见谅。
我把这个demo传上来,大家可以自己运行。(in vs2017rc .net4.6)
uwp - 上滑隐藏导航栏下滑显示的更多相关文章
- iOS---实现简书和知乎的上滑隐藏导航栏下拉显示导航栏效果
因为自己用简书和知乎比较多,所以对其导航栏的效果比较好奇,自己私下里找资料实现了一下.这个效果的关键点在于下方可供滑动的内容的便宜距离inset的改变,以及滑动的scrollview代理的执行,废话不 ...
- iOS 上滑隐藏导航,下滑显示导航,仿斗鱼导航效果
UItableView或 UIcollectionView 都是继承UIScrollView 滑动的时候,判断是上滑还是下滑 使用 UIScrollView 的代理方法 func scrollView ...
- AMScrollingNavbar框架(自动隐藏导航栏)使用简介
AMScrollingNavbar框架是一个可以上拉隐藏导航栏和下拉显示导航栏的框架,这个开源框架的调用也很简单,本章节就给大家介绍一下这个框架的用法. 一.下载及导入框架 AMScrollingNa ...
- iOS 滑动隐藏导航栏-三种方式
/** 1隐藏导航栏-简单- */ self.navigationController.hidesBarsOnSwipe = YES; /** 2隐藏导航栏-不随tableView滑动消失效果 ...
- 安卓ROOT后禁用/隐藏导航栏/虚拟按键
安卓ROOT后禁用/隐藏导航栏/虚拟按键 提醒:提前装好EASY TOUCH 等类似工具. 用ROOT EXPLORER 或 ROOT BROWSER system\bulid.prop 最后加一行: ...
- Material Design之CoordinatorLayout+AppBarLayout实现上滑隐藏ToolBar
ok,今天继续更新Material Design系列!!! 废话不说,先看看效果图吧: 好了,现在来讲讲上图是怎么实现的吧!讲之前先讲讲几个控件: CoordinatorLayout 该控件也是De ...
- iOS 隐藏导航栏下的黑线
一.找到导航栏下的黑线 // 寻找导航栏下的黑线 - (UIImageView *)findHairlineImageViewUnder:(UIView *)view { if ([view isKi ...
- iOS侧滑返回到隐藏导航栏的VC,导航栏会闪现一次
VCA:是一个隐藏导航栏的页面:VCA在ViewWillAppear生命周期函数中设置导航栏隐藏: //隐藏导航栏 [self.navigationController setNavigationBa ...
- iOS 优雅地隐藏导航栏NavigationBar (Objc)
@interface FSViewController () <UINavigationControllerDelegate> @end @implementation FSViewCon ...
随机推荐
- AE地图查询
原文 AE地图查询 地图查询主要有两种查询:空间查询和属性查询 所用到知识点: 1 Cursor(游标)对象 本质上是一个指向数据的指针,本身不包含数据内容,提供一个连接到ROW对象或者要素对象(F ...
- 07_android入门_採用HttpClient的POST方式、GET方式分别实现登陆案例
1.简单介绍 HttpClient 是 Apache Jakarta Common 下的子项目,能够用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,而且它支持 HTTP 协议 ...
- swift项目第六天:中间发布按钮的封装以及监听点击事件
import UIKit /* 总结:1:给UIButton写分类,新建文件swiftFile,一般为了区分起名字都是名字-Extension,要想调用UI控件需要导入 import UIKit框架, ...
- bootstrap课程2 bootstrap的栅格系统的主要作用是什么
bootstrap课程2 bootstrap的栅格系统的主要作用是什么 一.总结 一句话总结:响应式布局(就是适应不同的屏幕,手机,平板,电脑) 1.bootstrap的栅格系统如何使用? row ...
- 神经网络 vs 大脑
海马区域(负责记忆的关键区域) 0. 常见概念 神经递质:neurotransmitter 在突触传递中是担当"信使"的特定化学物质.简称递质. 重要的神经递质有:乙酰胆碱: 1. ...
- wordpress-nas
- bc -l 对于 %取模计算出错
https://yq.aliyun.com/articles/279384 expr % expr The result of the expression is the "rema ...
- Hadoop基本原理之一:MapReduce 分类: A1_HADOOP 2014-08-17 19:26 1113人阅读 评论(0) 收藏
1.为什么需要Hadoop 目前,一块硬盘容量约为1TB,读取速度约为100M/S,因此完成一块硬盘的读取需时约2.5小时(写入时间更长).若把数据放在同一硬盘上,且全部数据均需要同一个程序进行处理, ...
- Java编程思想第四版 *第五章 个人练习
练习3:(1)创建一个带默认构造器(即无參构造器)的类.在构造器中打印一条消息.为这个类创建一个对象.P116 public class Test{ public Test(){ System.out ...
- Thinking in UML 学习笔记(四)——UML核心视图之活动图
在UML中活动图的本质就是流程图,它描述了为了完成某一个目标需要做的活动以及这些互动的执行顺序.UML中有两个层面的活动图,一种用于描述用例场景,另一种用于描述对象交互. 活动图只是我们用来描述业务目 ...
