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的更多相关文章

  1. UWP开发入门(十)——通过继承来扩展ListView

    本篇之所以起这样一个名字,是因为重点并非如何自定义控件,不涉及创建CustomControl和UserControl使用的Template和XAML概念.而是通过继承的方法来扩展一个现有的类,在继承的 ...

  2. UWP开发入门(四)——自定义CommandBar

    各位好,再次回到UWP开发入门系列,刚回归可能有些不适应,所以今天我们讲个简单的,自定义CommandBar,说通俗点就是自定义类似AppBarButton的东西,然后扔到CommandBar中使用. ...

  3. UWP开发入门(十六)——常见的内存泄漏的原因

    本篇借鉴了同事翔哥的劳动成果,在巨人的肩膀上把稿子又念了一遍. 内存泄漏的概念我这里就不说了,之前<UWP开发入门(十三)——用Diagnostic Tool检查内存泄漏>中提到过,即使有 ...

  4. UWP开发入门系列笔记之(一):UWP初览

    标签: 随着微软Build2015带来的好消息,Win10正式版发布的日子已经离我们越来越近了,我们也终于欣喜地看到:一个统一的Windows平台对于开发人员来说充满了吸引力,这局棋下的好大的说--于 ...

  5. UWP开发入门(25)——通过Radio控制Bluetooth, WiFi

    回顾写了许久的UWP开发入门,竟然没有讲过通过Windows.Devices.Radios.Radio来控制Bluetooth和WiFi等功能的开关.也许是因为相关的API设计的简单好用,以至于被我给 ...

  6. UWP开发入门(二十三)——WebView

    本篇讨论在UWP开发中使用WebView控件时常见的问题,以及一些小技巧. WebView是实际开发中常用的控件,很多大家抱怨的套网页的应用都是通过WebView来实现的.这里要澄清一个问题,套网页的 ...

  7. UWP开发入门(二十一)——保持Ui线程处于响应状态

    GUI的程序有时候会因为等待一个耗时操作完成,导致界面卡死.本篇我们就UWP开发中可能遇到的情况,来讨论如何优化处理. 假设当前存在点击按钮跳转页面的操作,通过按钮打开的新页面,在初始化过程中存在一些 ...

  8. UWP开发入门(十一)——Attached Property的简单应用

    UWP中的Attached Property即附加属性,在实际开发中是很常见的,比如Grid.Row: <Grid Background="{ThemeResource Applica ...

  9. UWP开发入门(七)——下拉刷新

    本篇意在给这几天Win10 Mobile负面新闻不断的某软洗地,想要证明实现一个简单的下拉刷新并不困难.UWP开发更大的困难在于懒惰,缺乏学习的意愿.而不是“某软连下拉刷新控件都没有”这样的想法. 之 ...

随机推荐

  1. hex文件和bin文件区别

    HEX文件和BIN文件是我们经常碰到的2种文件格式.因为自己也是新手,所以一直对这两个文件懵懵懂懂,不甚了解,最近在做STM32单片机的IAP更新,其中要考虑HEX文件和BIN文件,所以需要学习下这两 ...

  2. Cordova+Vue构建Hybrid APP简易实操

    当下APP市场,因为Native APP开发成本高,Web APP不稳定,混合开发APP大行其道,成为越来越多开发者的首选.Hybrid APP开发框架也比较多,Weex.Ionic.PhoneGap ...

  3. ElasticSearch、Kibana 启动(含前台和后台启动、停止)(含界面浏览)

    前提: Elasticsearch-2.4.3的下载(图文详解) Elasticsearch-2.4.3的单节点安装(多种方式图文详解) Elasticsearch-2.4.3的3节点安装(多种方式图 ...

  4. VMware 克隆网卡无法启动

    问题描述: 最近学习 hadoop,环境准备搭建在虚拟机之上,装好一台虚拟机克隆完成后,网卡无法启动. 多年前,初学 Linux 的时候,就遇到过这个问题,记录的笔记找不到了,简单记录一下. shel ...

  5. Python运维开发基础01-语法基础

    标签(空格分隔): Mr.chen之Python3.0执教笔记(QQ:215379068) --仅供北大青鸟内部学习交流使用 开发不是看出来的,开发一定是练出来的: 想学好开发,没有捷径可走,只有不断 ...

  6. CTC Loss原理

    https://blog.csdn.net/left_think/article/details/76370453 1. 背景介绍  在传统的语音识别的模型中,我们对语音模型进行训练之前,往往都要将文 ...

  7. C++:初始化列表

    有两种原因需要使用初始化列表: 让我们先看一下第一个原因——必要性.(1)对另一个类成员的初始化,(2)成员是一个常量对象,(3)成员是引用.根本原因:编译器总是确保所有成员对象在构造函数体执行之前( ...

  8. 优化 resolv.conf

    DNS lookup 在把域名解析成 IP 过程中耽误了不少时间,尤其是访问比较复杂的网站的时候,比如某个页面包含多个 url,一次请求需要做多次 DNS 解析,并且从用户浏览器到 DNS serve ...

  9. Window Application has "update" key words

    Error Qt Creater:console error:Failed to start program. Path or permissions wrong? Description 在使用Qt ...

  10. MySQL 系列(三)事务

    MySQL 系列(三)事务 一组要么同时执行成功,要么同时执行失败的 SQL 语句.是数据库操作的一个执行单元! 事务开始于: 连接到数据库上,并执行条 DML 语句(INSERT. UPDATE 或 ...