UWP开发入门(二)——RelativePanel
RelativePanel也是Win10 UWP新增的控件,和上篇提到的SplitView一样在UWP的UI布局起到非常重要的作用。说句实在话,这货其实就是为了UWP的Adaptive UI而特意增加的,由于他的功能和DockPanel有相当的重叠,以至于DockPanel被从Win10 SDK中被撸掉了……太惨了……
为什么说RelativePanel可以替代DockPanel,我们可以先从几个比较重要的属性看起:AlignLeftWithPanel,AlignRightWithPanel,AlignTopWithPanel,AlignBottomWithPanel。这几个属性如果是True的话,看上去的效果分明就是原先的DockPanel.Left,Right,Top,Bottom。我们先来看原先可以用DockPanel实现的下图,采用RelativePanel是如何编写的:

<RelativePanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" >
<Button x:Name="ButtonHamburger" FontFamily="{ThemeResource SymbolThemeFontFamily}" Content="" RelativePanel.AlignLeftWithPanel="True"></Button>
<TextBlock Text="类别" RelativePanel.RightOf="ButtonHamburger" RelativePanel.AlignVerticalCenterWith="ButtonHamburger" Margin="10,0,0,0"></TextBlock>
<Button FontFamily="{ThemeResource SymbolThemeFontFamily}" Content="" RelativePanel.LeftOf="ButtonAdd"/>
<Button x:Name="ButtonAdd" FontFamily="{ThemeResource SymbolThemeFontFamily}" Content="" RelativePanel.AlignRightWithPanel="True"/>
</RelativePanel>
RelativePanel中共有三个Button,一个TextBlock。分别靠左右对齐,用到了RelativePanel的几个附加属性:AlignLeftWithPanel,RightOf,LeftOf,AlignRightWithPanel。这里还有一点要注意一下,TextBlock为了实现纵向的居中对齐,使用了AlignVerticalCenterWith,有兴趣的同学可以试一下,在RelativePanel里VerticalAlignment优先级较低,仅在空间不足以显示控件时才起到居中对齐的作用。
有的童鞋会说以上的效果即使用Grid也是可以实现的,话是没有错啦,但在UWP开发中,RelativePanel一般都是要配合AdaptiveTrigger来实现自适应布局的,比如下面两张图对比:


在平板或者桌面模式,采用左右的菜单布局,而在手机则变成上下菜单布局,在UWP中实现这种动态变化的效果,完全可以通过纯XAML来实现:
<RelativePanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState >
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="401" />
</VisualState.StateTriggers>
</VisualState>
<VisualState >
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="0" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="RelativeNavigation.(RelativePanel.AlignTopWithPanel)" Value="False"></Setter>
<Setter Target="RelativeNavigation.(RelativePanel.AlignRightWithPanel)" Value="True"></Setter>
<Setter Target="ButtonHome.(RelativePanel.AlignTopWithPanel)" Value="False"></Setter>
<Setter Target="ButtonHome.(RelativePanel.AlignLeftWithPanel)" Value="True"></Setter>
<Setter Target="ButtonMessage.(RelativePanel.Below)" Value=""></Setter>
<Setter Target="ButtonMessage.(RelativePanel.RightOf)" Value="ButtonHome"></Setter>
<Setter Target="ButtonAdd.(RelativePanel.Below)" Value=""></Setter>
<Setter Target="ButtonAdd.(RelativePanel.RightOf)" Value="ButtonMessage"></Setter>
<Setter Target="ButtonFind.(RelativePanel.Below)" Value=""></Setter>
<Setter Target="ButtonFind.(RelativePanel.RightOf)" Value="ButtonAdd"></Setter>
<Setter Target="ButtonMe.(RelativePanel.Below)" Value=""></Setter>
<Setter Target="ButtonMe.(RelativePanel.RightOf)" Value="ButtonFind"></Setter>
<Setter Target="GridContent.(RelativePanel.AlignBottomWithPanel)" Value="False"></Setter>
<Setter Target="GridContent.(RelativePanel.AlignLeftWithPanel)" Value="True"></Setter>
<Setter Target="GridContent.(RelativePanel.AlignBottomWith)" Value="RelativeNavigation"></Setter>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<RelativePanel x:Name="RelativeNavigation" RelativePanel.AlignLeftWithPanel="True" RelativePanel.AlignBottomWithPanel="True" RelativePanel.AlignTopWithPanel="True" Background="LightGray">
<Button x:Name="ButtonHome" FontFamily="{ThemeResource SymbolThemeFontFamily}" Content="" RelativePanel.AlignTopWithPanel="True"/>
<Button x:Name="ButtonMessage" FontFamily="{ThemeResource SymbolThemeFontFamily}" Content="" RelativePanel.Below="ButtonHome"/>
<Button x:Name="ButtonFind" FontFamily="{ThemeResource SymbolThemeFontFamily}" Content="" RelativePanel.Below="ButtonMessage"/>
<Button x:Name="ButtonMe" FontFamily="{ThemeResource SymbolThemeFontFamily}" Content="" RelativePanel.Below="ButtonFind"/>
<Button x:Name="ButtonAdd" FontFamily="{ThemeResource SymbolThemeFontFamily}" Content="" Background="Orange" RelativePanel.Below="ButtonMe"/>
</RelativePanel>
<Grid x:Name="GridContent" RelativePanel.AlignRightWithPanel="True" RelativePanel.AlignBottomWithPanel="True" RelativePanel.AlignTopWithPanel="True" RelativePanel.RightOf="RelativeNavigation" >
<TextBlock Text="我是一个水印" Foreground="LightGray" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
</Grid>
</RelativePanel>
看上去啰里啰唆写了一大堆,主要还是为了展示RelativePanel的用法,并不是最优的写法,如果能提供各位一丝丝的灵感,那俺就很满意了。
UWP开发入门(二)——RelativePanel的更多相关文章
- UWP开发入门(十)——通过继承来扩展ListView
本篇之所以起这样一个名字,是因为重点并非如何自定义控件,不涉及创建CustomControl和UserControl使用的Template和XAML概念.而是通过继承的方法来扩展一个现有的类,在继承的 ...
- UWP开发入门(四)——自定义CommandBar
各位好,再次回到UWP开发入门系列,刚回归可能有些不适应,所以今天我们讲个简单的,自定义CommandBar,说通俗点就是自定义类似AppBarButton的东西,然后扔到CommandBar中使用. ...
- UWP开发入门(十六)——常见的内存泄漏的原因
本篇借鉴了同事翔哥的劳动成果,在巨人的肩膀上把稿子又念了一遍. 内存泄漏的概念我这里就不说了,之前<UWP开发入门(十三)——用Diagnostic Tool检查内存泄漏>中提到过,即使有 ...
- UWP开发入门系列笔记之(一):UWP初览
标签: 随着微软Build2015带来的好消息,Win10正式版发布的日子已经离我们越来越近了,我们也终于欣喜地看到:一个统一的Windows平台对于开发人员来说充满了吸引力,这局棋下的好大的说--于 ...
- UWP开发入门(25)——通过Radio控制Bluetooth, WiFi
回顾写了许久的UWP开发入门,竟然没有讲过通过Windows.Devices.Radios.Radio来控制Bluetooth和WiFi等功能的开关.也许是因为相关的API设计的简单好用,以至于被我给 ...
- UWP开发入门(二十三)——WebView
本篇讨论在UWP开发中使用WebView控件时常见的问题,以及一些小技巧. WebView是实际开发中常用的控件,很多大家抱怨的套网页的应用都是通过WebView来实现的.这里要澄清一个问题,套网页的 ...
- UWP开发入门(二十一)——保持Ui线程处于响应状态
GUI的程序有时候会因为等待一个耗时操作完成,导致界面卡死.本篇我们就UWP开发中可能遇到的情况,来讨论如何优化处理. 假设当前存在点击按钮跳转页面的操作,通过按钮打开的新页面,在初始化过程中存在一些 ...
- UWP开发入门(十一)——Attached Property的简单应用
UWP中的Attached Property即附加属性,在实际开发中是很常见的,比如Grid.Row: <Grid Background="{ThemeResource Applica ...
- UWP开发入门(七)——下拉刷新
本篇意在给这几天Win10 Mobile负面新闻不断的某软洗地,想要证明实现一个简单的下拉刷新并不困难.UWP开发更大的困难在于懒惰,缺乏学习的意愿.而不是“某软连下拉刷新控件都没有”这样的想法. 之 ...
随机推荐
- 33_java之类加载器和反射
01类加载器 * A.类的加载 当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载,连接,初始化三步来实现对这个类进行初始化. * a 加载 * 就是指将class文件读入内存,并为之 ...
- python中tornado的第一个例子
1 先安装tornado pip install tornado 2 新建tor.py 记住不能建立 tornado.py 这样的名字 不然会报错 ImportError: No module n ...
- Android Studio中由于gradle插件版本和gradle版本对应关系导致的编译失败的问题
今天在Android Studio中导入新项目,import之后编译报错,报错信息基本都是和版本相关,查询gradle版本相关知识,了解到gradle插件版本和gradle版本有相应的匹配关系,对应如 ...
- libcurl使用心得-包括下载文件不存在处理相关(转)
libcurl使用心得 Libcurl为一个免费开源的,客户端url传输库,支持FTP,FTPS,TFTP,HTTP,HTTPS,GOPHER,TELNET,DICT,FILE和LDAP,跨平台,支持 ...
- Jboss:The LogManager was not properly installed (you must set the "java.util.logging.manager" system prop
可能是jboss的服务器版本选择不对 ,比如我本地的Jboss服务器版本是 jboss-as-web-7.0.2.Final,选择的服务器版本是JBOOS V7.1 Runtime ,就会报上面 ...
- linux 同步IO: sync、fsync与fdatasync
[linux 同步IO: sync.fsync与fdatasync] 传统的UNIX实现在内核中设有缓冲区高速缓存或页面高速缓存,大多数磁盘I/O都通过缓冲进行.当将数据写入文件时,内核通常先将该数据 ...
- 理解ServletRequest和ServletResponse
博客地址:http://blog.51cto.com/lavasoft/275586
- token的作用及实现原理
1:首先,先了解一下request和session的区别request 指在一次请求的全过程中有效,即从http请求到服务器处理结束,返回响应的整个过程,存放在HttpServletRequest对象 ...
- 使用SQL查询所有数据库名和表名
使用SQL查询所有数据库名和表名 MySQL中查询所有数据库名和表名 查询所有数据库 show databases; 1 1 查询指定数据库中所有表名 select table_name from i ...
- Docker使用link建立容器之间的连接
我们在使用Docker的时候,经常可能需要连接到其他的容器,比如:web服务需要连接数据库.按照往常的做法,需要先启动数据库的容器,映射出端口来,然后配置好客户端的容器,再去访问.其实针对这种场景,D ...