热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

UWP开发入门(二)——RelativePanel

RelativePanel也是Win10UWP新增的控件,和上篇提到的SplitView一样在UWP的UI布局起到非常重要的作用。说句实在话,这货其实就是为了UWP的AdaptiveUI而特意

  RelativePanel也是Win10 UWP新增的控件,和上篇提到的SplitView一样在UWPUI布局起到非常重要的作用。说句实在话,这货其实就是为了UWPAdaptive 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="&#xE700;" RelativePanel.AlignLeftWithPanel="True">Button>
        <TextBlock Text="类别" RelativePanel.RightOf="ButtonHamburger" RelativePanel.AlignVerticalCenterWith="ButtonHamburger" Margin="10,0,0,0">TextBlock>
        <Button FontFamily="{ThemeResource SymbolThemeFontFamily}" Content="&#xE11A;" RelativePanel.LeftOf="ButtonAdd"/>
        <Button x:Name="ButtonAdd" FontFamily="{ThemeResource SymbolThemeFontFamily}" Content="&#xE710;" RelativePanel.AlignRightWithPanel="True"/>
    RelativePanel>

  RelativePanel中共有三个Button,一个TextBlock。分别靠左右对齐,用到了RelativePanel的几个附加属性:AlignLeftWithPanelRightOfLeftOfAlignRightWithPanel。这里还有一点要注意一下,TextBlock为了实现纵向的居中对齐,使用了AlignVerticalCenterWith,有兴趣的同学可以试一下,在RelativePanelVerticalAlignment优先级较低,仅在空间不足以显示控件时才起到居中对齐的作用。

  有的童鞋会说以上的效果即使用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="&#xE10F;" RelativePanel.AlignTopWithPanel="True"/>
            <Button x:Name="ButtonMessage" FontFamily="{ThemeResource SymbolThemeFontFamily}" Content="&#xE119;" RelativePanel.Below="ButtonHome"/>
            <Button x:Name="ButtonFind" FontFamily="{ThemeResource SymbolThemeFontFamily}" Content="&#xE11A;" RelativePanel.Below="ButtonMessage"/>
            <Button x:Name="ButtonMe" FontFamily="{ThemeResource SymbolThemeFontFamily}" Content="&#xE13D;" RelativePanel.Below="ButtonFind"/>
            <Button x:Name="ButtonAdd" FontFamily="{ThemeResource SymbolThemeFontFamily}" Content="&#xE710;" 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的用法,并不是最优的写法,如果能提供各位一丝丝的灵感,那俺就很满意了。

 


推荐阅读
  • button进阶1.等级3的时候学习了用Grid(网格)布局排列。这次是button背景的引用学习圆角button。我发现学代码难的原因就是英语不好不懂什么意思。设置focysable属性为 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 本文介绍了在MFC下利用C++和MFC的特性动态创建窗口的方法,包括继承现有的MFC类并加以改造、插入工具栏和状态栏对象的声明等。同时还提到了窗口销毁的处理方法。本文详细介绍了实现方法并给出了相关注意事项。 ... [详细]
  • 如何在HTML中获取鼠标的当前位置
    本文介绍了在HTML中获取鼠标当前位置的三种方法,分别是相对于屏幕的位置、相对于窗口的位置以及考虑了页面滚动因素的位置。通过这些方法可以准确获取鼠标的坐标信息。 ... [详细]
  • Android自定义控件绘图篇之Paint函数大汇总
    本文介绍了Android自定义控件绘图篇中的Paint函数大汇总,包括重置画笔、设置颜色、设置透明度、设置样式、设置宽度、设置抗锯齿等功能。通过学习这些函数,可以更好地掌握Paint的用法。 ... [详细]
  • 本文介绍了pack布局管理器在Perl/Tk中的使用方法及注意事项。通过调用pack()方法,可以控制部件在显示窗口中的位置和大小。同时,本文还提到了在使用pack布局管理器时,应注意将部件分组以便在水平和垂直方向上进行堆放。此外,还介绍了使用Frame部件或Toplevel部件来组织部件在窗口内的方法。最后,本文强调了在使用pack布局管理器时,应避免在中间切换到grid布局管理器,以免造成混乱。 ... [详细]
  • 本文详细介绍了Android中的坐标系以及与View相关的方法。首先介绍了Android坐标系和视图坐标系的概念,并通过图示进行了解释。接着提到了View的大小可以超过手机屏幕,并且只有在手机屏幕内才能看到。最后,作者表示将在后续文章中继续探讨与View相关的内容。 ... [详细]
  • 带添加按钮的GridView,item的删除事件
    先上图片效果;gridView无数据时显示添加按钮,有数据时,第一格显示添加按钮,后面显示数据:布局文件:addr_manage.xml<?xmlve ... [详细]
  • 颜色迁移(reinhard VS welsh)
    不要谈什么天分,运气,你需要的是一个截稿日,以及一个不交稿就能打爆你狗头的人,然后你就会被自己的才华吓到。------ ... [详细]
  • 如何用Matlab快速画出带有3D渲染效果的复杂曲面
    简要地介绍了一下如何用Matlab快速画出带有3D渲染效果的复杂曲面图,包括三维曲面绘制、光线、材质、着色等等控制,以及如何 ... [详细]
  • 最近百度了好几种方式都没有效果针对这种a标签直接open的方式我也是醉了 因为要对这个导出文件大小进行限制,当文件大于100mb的时候,就会有提示并且不让下载对于这种前端计算是很难 ... [详细]
  • 本文由编程笔记#小编为大家整理,主要介绍了24.QTableView函数使用,右击菜单实现相关的知识,希望对你有一定的参考价值。QTableViewview(this); ... [详细]
  • [WPF] 圆形等待效果
    自己做着玩儿的,留着以后用,效果类似下面的GIF动画。<GridWidth"35"Height"35"><G ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • 准备工作参考我的另一篇利用githubactions自动部署gradle构建的springboot项目打包的war包到tomcat服务器,这里直接上配置, ... [详细]
author-avatar
啊哈哈
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有