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. 使用Fiddler对IPhone手机的应用数据进行抓包分析

    原文出自: http://www.cr173.com/html/20064_1.html Fiddler能捕获ISO设备发出的请求,比如IPhone, IPad, MacBook. 等等苹果的设备.  ...

  2. readlink 获取当前进程对应proc/self/exe

    [readlink 获取当前进程对应proc/self/exe] shell中  readlink /proc/self/exe READLINK(2)NAME       readlink - re ...

  3. Android开发之获取系统所有进程信息。

    最近在做一个app,有一个进程管理模块用于管理系统中正在运行的进程,并且可以关闭进程进行加速手机的功能,基本把它实现了出来.界面的效果都是自己写的,虽然有BUG,但是基本上能满足需求,后期我会改进BU ...

  4. Python id() 函数

    Python id() 函数  Python 内置函数 描述 id() 函数用于获取对象的内存地址. 语法 id 语法: id([object]) 参数说明: object -- 对象. 返回值 返回 ...

  5. 数论知识总结——史诗大作(这是一个flag)

    1.快速幂 计算a^b的快速算法,例如,3^5,我们把5写成二进制101,3^5=3^1*1+3^2*2+3^4*1 ll fast(ll a,ll b){ll ans=;,a=mul(a,a)))a ...

  6. Cocoa Touch(六):App运行机制 NSRunLoop, KVC, KVO, Notification, ARC

    事件循环NSRunLoop 1.run loop概念 NSRunLoop类封装了线程进入事件循环的过程,一个runloop实例就表示了一个线程的事件循环.更具体的说,在iOS开发框架中,线程每次执行完 ...

  7. C# 将Word,Execl,PPT,Project, 文件转成PDF, 不依赖Office!!

    git 地址 https://gitee.com/bandung/Execl_WordTOPDF.git 包括了各种破解的dll Word转PDF 挨个引用 Word转PDF public void ...

  8. Laravel 引入自定义类库或第三方类库

    强烈建议引入的类 都是含有命名空间的,这样使用起来就不会出现重名的情况.!!当然,没有命名空间也可以使用,就是类名字(非文件名)最好复杂一些.(重复也不要紧,程序会自己判断) laravel5.4中如 ...

  9. 解决 Windows 系统使用 Homestead 运行 Laravel 本地项目响应缓慢问题

    laravel-china.com: https://laravel-china.org/articles/9009/solve-the-slow-response-problem-of-window ...

  10. 界面设计中如何增强CTA按钮召唤力?

    以下内容由Mockplus(摹客)团队翻译整理,仅供学习交流,Mockplus是更快更简单的原型设计工具. 网页和软件应用之类数字产品的有效交互系统一般是由拥有各种任务和功能的小元素构成.而为创建更加 ...