Extensions in UWP Community Toolkit - ViewExtensions
概述
UWP Community Toolkit Extensions 中有一个为 View 提供的扩展 - View Extensions,本篇我们结合代码详细讲解 View Extensions 的实现。
View Extensions 包括了 ApplicationViewExtensions,StatusBarExtensions 和 TitleBarExtensions,让开发者可以方便的定制 AppView,StatusBar 和 TitleBar 的样式,接下来看看官方示例的截图:

Doc: https://docs.microsoft.com/zh-cn/windows/uwpcommunitytoolkit/extensions/viewextensions
Namespace: Microsoft.Toolkit.Uwp.UI.Extensions; Nuget: Microsoft.Toolkit.Uwp.UI;
开发过程
代码分析
由于 ViewExtensions 分为 ApplicationViewExtensions,StatusBarExtensions 和 TitleBarExtensions 三个部分,我们分别来看一下:
1. ApplicationViewExtensions
先来看一下 ApplicationViewExtensions 的结构:

虽然有两个类组成,但其实 ApplicationView.cs 类是 Obsolete 的,所以现在在使用的是 ApplicationViewExtensions.cs,我们主要看一下这个类,先看一下类结构:

类的功能比较简单,我们主要来看这几个针对 Page 的附加属性对应的 get 和 set 方法:
- Title 对应 GetTitle(page) 和 SetTitle(page, value) - 获取和设置 App 标题,主要处理逻辑是通过 GetApplicationView() 获取 applicationView,然后再获取或设置 Title 属性;
- ExtendViewIntoTitleBar 对应 GetExtendViewIntoTitleBar(page) 和 SetExtendViewIntoTitleBar(page, value) - 获取和设置是否扩展视图到标题栏的布尔值,主要处理逻辑是通过 GetCoreApplicationView() 获取 CoreApplicationView,然后再获取或设置这个属性,如果为 True,那么 App 的 UI 会占据 TitleBar 的位置;
- BackButtonVisibility 对应 GetBackButtonVisibility(page) 和 SetBackButtonVisibility(page, value) - 获取和设置后退按钮是否可用,主要处理逻辑是通过 GetSystemNavigationManager() 来获取 SystemNavigationManager,然后再设置或获取这个属性;
2. StatusBarExtensions
先来看一下 StatusBarExtensions 的结构:

和 ApplicationViewExtensions 类似,StatusBar.cs 类是 Obsolete 的,所以现在在使用的是 StatusBarExtensions.cs,我们主要看一下这个类,先看一下类结构:

类的功能比较简单,我们主要来看这几个针对 Page 的附加属性对应的 get 和 set 方法:
- BackgroundColor 对应 GetBackgroundColor(page) 和 SetBackgroundColor(page, color) - 获取和设置 StatusBar 的背景颜色,主要通过 GetStatusBar() 获得 StatusBar 实例,然后获取或设置 BackgroundColor 属性;
- ForegroundColor 对应 GetForegroundColor(page) 和 SetForegroundColor(page, color) - 获取和设置 StatusBar 的前景颜色,主要通过 GetStatusBar() 获得 StatusBar 实例,然后获取或设置 ForegroundColor 属性;
- BackgroundOpaticy 对应 GetBackgroundOpaticy(page) 和 SetBackgroundOpaticy(page, color) - 获取和设置 StatusBar 的背景透明度,主要通过 GetStatusBar() 获得 StatusBar 实例,然后获取或设置 BackgroundOpaticy 属性;
- IsVisible 对应 GetIsVisible(page) 和 SetIsVisible(page, double) - 获取和设置 StatusBar 是否可见,获取方法通过获取 OccludedRect Height 的高度来判断是否可见,因为 InputPane 的 VIsible 属性只在 XBox 有效;设置是通过 Page 的 IsVisibleProperty 属性来设置;IsVisibleProperty 是类中定义的依赖属性,改变时触发 OnIsVisibleChanged 事件;
来看一下 OnIsVisibleChanged 事件的处理方法,通过调用 StatusBar 的 ShowAsync() 和 HideAsync() 方法来设置 StatusBar 的可见和不可见;
private static async void OnIsVisibleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var statusBar = GetStatusBar();
if (statusBar == null)
{
return;
}
bool isVisible = (bool)e.NewValue;
if (isVisible)
{
await statusBar.ShowAsync();
}
else
{
await statusBar.HideAsync();
}
}
3. TitleBarExtensions
先来看一下 TitleBarExtensions 的结构:

和 ApplicationViewExtensions 类似,TitleBar.cs 类是 Obsolete 的,所以现在在使用的是 TitleBarExtensions.cs,我们主要看一下这个类,先看一下类结构:

类的功能比较简单,我们主要来看这几个针对 Page 的附加属性对应的 get 和 set 方法:
- BackgroundColor 对应 GetBackgroundColor(page) 和 SetBackgroundColor(page, color) - 获取和设置 TitleBar 的背景色,主要通过 GetTitleBar() 方法获得 TitleBar 实例,然后获取或设置 BackgroundColor 属性;在显示上会覆盖 StatusBar 的对应属性;
- ButtonBackgroundColor 对应 GetButtonBackgroundColor(page) 和 SetButtonBackgroundColor(page, color) - 获取和设置 TitleBar 的右上角三个按钮的背景色,主要通过 GetTitleBar() 方法获得 TitleBar 实例,然后获取或设置 ButtonBackgroundColor 属性;
- ButtonForegroundColor 对应 GetButtonForegroundColor(page) 和 SetButtonForegroundColor(page, color) - 获取和设置 TitleBar 的右上角三个按钮的前景色,主要通过 GetTitleBar() 方法获得 TitleBar 实例,然后获取或设置 ButtonForegroundColor 属性;
- ButtonHoverBackgroundColor 对应 GetButtonHoverBackgroundColor(page) 和 SetButtonHoverBackgroundColor(page, color) - 获取和设置 TitleBar 的右上角三个按钮的鼠标悬浮背景色,主要通过 GetTitleBar() 方法获得 TitleBar 实例,然后获取或设置 ButtonHoverBackgroundColor 属性;
- ButtonHoverForegroundColor 对应 GetButtonHoverForegroundColor(page) 和 SetButtonHoverForegroundColor(page, color) - 获取和设置 TitleBar 的右上角三个按钮的鼠标悬浮前景色,主要通过 GetTitleBar() 方法获得 TitleBar 实例,然后获取或设置 ButtonHoverForegroundColor 属性;
- ButtonInactiveBackgroundColor 对应 GetButtonInactiveBackgroundColor(page) 和 SetButtonInactiveBackgroundColor(page, color) - 获取和设置 TitleBar 的右上角三个按钮在窗口非活动状态时的背景色,主要通过 GetTitleBar() 方法获得 TitleBar 实例,然后获取或设置 ButtonInactiveBackgroundColor 属性;
- ButtonInactiveForegroundColor 对应 GetButtonInactiveForegroundColor(page) 和 SetButtonInactiveForegroundColor(page, color) - 获取和设置 TitleBar 的右上角三个按钮在窗口非活动状态时的前景色,主要通过 GetTitleBar() 方法获得 TitleBar 实例,然后获取或设置 ButtonInactiveForegroundColor 属性;
- ButtonPressedBackgroundColor 对应 GetButtonPressedBackgroundColor(page) 和 SetButtonPressedBackgroundColor(page, color) - 获取和设置 TitleBar 的右上角三个按钮点击时的背景色,主要通过 GetTitleBar() 方法获得 TitleBar 实例,然后获取或设置 ButtonPressedBackgroundColor 属性;
- ButtonPressedForegroundColor 对应 GetButtonPressedForegroundColor(page) 和 SetButtonPressedForegroundColor(page, color) - 获取和设置 TitleBar 的右上角三个按钮点击时的前景色,主要通过 GetTitleBar() 方法获得 TitleBar 实例,然后获取或设置 ButtonPressedForegroundColor 属性;
- ForegroundColor 对应 GetForegroundColor(page) 和 SetForegroundColor(page, color) - 获取和设置 TitleBar 的前景色,主要通过 GetTitleBar() 方法获得 TitleBar 实例,然后获取或设置 ForegroundColor 属性;在显示上会覆盖 StatusBar 的对应属性;
- InactiveBackgroundColor 对应 GetInactiveBackgroundColor(page) 和 SetInactiveBackgroundColor(page, color) - 获取和设置 TitleBar 在窗口非活动时的背景色,主要通过 GetTitleBar() 方法获得 TitleBar 实例,然后获取或设置 InactiveBackgroundColor 属性;在显示上会覆盖 StatusBar 的对应属性;
- InactiveForegroundColor 对应 GetInactiveForegroundColor(page) 和 SetInactiveForegroundColor(page, color) - 获取和设置 TitleBar 在窗口非活动时的前景色,主要通过 GetTitleBar() 方法获得 TitleBar 实例,然后获取或设置 InactiveForegroundColor 属性;在显示上会覆盖 StatusBar 的对应属性;
调用示例
我们定制了 AppView 的 Title,StatusBar 和 TitleBar 的样式,看到运行图和设置的一致;
<Page
x:Class="CommunityToolkitSample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:extensions="using:Microsoft.Toolkit.Uwp.UI.Extensions"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
extensions:ApplicationViewExtensions.Title="View Extensions"
extensions:TitleBarExtensions.BackgroundColor="Red"
extensions:TitleBarExtensions.ForegroundColor="Green"
extensions:TitleBarExtensions.ButtonBackgroundColor="Gray"
extensions:TitleBarExtensions.ButtonForegroundColor="White"
extensions:StatusBarExtensions.BackgroundColor="CornflowerBlue"
extensions:StatusBarExtensions.BackgroundOpacity="0.8"
extensions:StatusBarExtensions.ForegroundColor="White"
extensions:StatusBarExtensions.IsVisible="False"
mc:Ignorable="d">

总结
到这里我们就把 UWP Community Toolkit Extensions 中的 View Extensions 的源代码实现过程和简单的调用示例讲解完成了,希望能对大家更好的理解和使用这个扩展有所帮助。欢迎大家多多交流,谢谢!
最后,再跟大家安利一下 UWPCommunityToolkit 的官方微博:https://weibo.com/u/6506046490, 大家可以通过微博关注最新动态。
衷心感谢 UWPCommunityToolkit 的作者们杰出的工作,Thank you so much, UWPCommunityToolkit authors!!!
Extensions in UWP Community Toolkit - ViewExtensions的更多相关文章
- Extensions in UWP Community Toolkit - Overview
概述 UWP Community Toolkit 中有一个 Extensions 的集合,它们可以帮助开发者实现很多基础功能,省去自己造轮子的过程,本篇我们先来看一下 Extensions 的功能都 ...
- Extensions in UWP Community Toolkit - FrameworkElement Extensions
概述 UWP Community Toolkit Extensions 中有一个为FrameworkElement 提供的扩展 - FrameworkElement Extensions,本篇我们结合 ...
- Extensions in UWP Community Toolkit - Mouse Cursor
概述 UWP Community Toolkit Extensions 中有一个为 Mouse 提供的扩展 - Mouse Cursor Extensions,本篇我们结合代码详细讲解 Mouse C ...
- Extensions in UWP Community Toolkit - SurfaceDialTextbox
概述 UWP Community Toolkit Extensions 中有一个为TextBox 提供的 SurfaceDial 扩展 - SurfaceDialTextbox,本篇我们结合代码详细讲 ...
- Extensions in UWP Community Toolkit - Visual Extensions
概述 UWP Community Toolkit Extensions 中有一个为可视元素提供的扩展 - VisualExtensions,本篇我们结合代码详细讲解 VisualExtensions ...
- Extensions in UWP Community Toolkit - WebViewExtensions
概述 UWP Community Toolkit Extensions 中有一个为 WebView 提供的扩展 - WebViewExtensions,本篇我们结合代码详细讲解 WebView Ext ...
- Extensions in UWP Community Toolkit - ListViewExtensions
概述 UWP Community Toolkit Extensions 中有一个为 ListView 提供的扩展 - ListViewExtensions,本篇我们结合代码详细讲解 ListView ...
- New UWP Community Toolkit
概述 UWP Community Toolkit 是一个 UWP App 自定义控件.应用服务和帮助方法的集合,能够很大程度的简化和指引开发者的开发工作,相信广大 UWPer 并不陌生. 下面是截取自 ...
- Animations in UWP Community Toolkit - Overview
概述 UWP Community Toolkit 中有一个 Animations 的集合,它们可以帮助开发者实现很多的动画,本篇我们先来看一下 Animations 的功能都有哪些,再后面会针对每一 ...
随机推荐
- 写了个批量查询qs的软件
因为需要,自己写了个批量查询qs的小软件.从网站中抓出需要的数据,格式化显示: 对字符串进行检测处理,先用Replace函数去掉字符串的空格,再用正则表达式匹配,返回匹配的字符串,如果没有匹配,则返回 ...
- jquery ajax 返回的json对象 新增属性值(干货)
$.ajax({ type:"GEt'; url:"你的地址", data:{"你的字段","字段值"} success:funt ...
- Hibernate 一对一关联映射,mappedBy参数解析
在最近java,SSH框架的学习中遇到了这样的一个问题,在Hibernate的开发中一对一关联映射的单向关联,主表会在次表新增一列次表的主键如下图,但是在双向关联中次表不会在表中创建主表的主键一列,这 ...
- 1-3 hibernate核心对象关系映射 xxx.hbm.xml
详见 http://www.cnblogs.com/biehongli/p/6532800.html 1 <?xml version="1.0" encoding='utf ...
- Java CAS机制详解
CAS目的: 在多线程中为了保持数据的准确性,避免多个线程同时操作某个变量,很多情况下利用关键字synchronized实现同步锁,使用synchronized关键字修可以使操作的线程排队等待运行,可 ...
- java基础学习系列二
循环语句 1,for(){} 2,while(){} 3,do{}while() continue和break用法 break是结束循环 continue结束本次循环
- Spring中配置数据源常用的形式
不管采用何种持久化技术,都需要定义数据源.Spring中提供了4种不同形式的数据源配置方式: spring自带的数据源(DriverManagerDataSource),DBCP数据源,C3P0数据源 ...
- alpha-咸鱼冲刺day9-紫仪
总汇链接 一,合照 emmmmm.自然还是没有的. 二,项目燃尽图 三,项目进展 大概差不多了.不过提交似乎又出了问题正在修改ing 四,问题困难 页面整合啥的凑合一下.因为后面跟着学长速成的PH ...
- 2017-2018-1 我爱学Java 第三周 作业
Team Presentation 团队展示 队员学号 队名 团队项目描述 队员风采 团队首次合照 团队的特色描述 团队初步合作 前两周合作过程中的优缺点 如何改进 团队选题 确立,建立和初步熟悉团队 ...
- PostgreSQL 配置安装
Mac 安装 http://postgresapp.com/ 创建和删除数据库用户 对应命令如下(在postgres=# 环境下):1.查看数据库用户列表: \du2.创建数据库用户: create ...