WP8.1学习系列(第十一章)——中心控件Hub开发指南

重要的 API
使用 Hub 控件创建一个进入应用的入口页。Hub 控件在丰富的平移视图中显示内容,这样用户一眼就能看见新鲜有趣的内容,从而吸引他们深入了解你的应用中的更多内容。
先决条件
- 查看并了解 Windows 导航模式。
- 查看并了解中心控件指南。
- 我们假定你可以向使用 C++、C# 或 Visual Basic 的基本 Windows 应用商店应用中添加控件。要了解如何添加控件,请参阅快速入门:添加控件和处理事件。
什么是中心控件?
中心页面是用户进入应用的入口点。它们为用户提供了一种查看新鲜有趣的内容的引人注目的方式,从而吸引他们深入了解你的应用中的更多内容。中心显示不同的内容类别,每个类别映射到应用的分区页中。理想情况下,每个分区都显示内容或功能。中心则提供许多视觉变化、吸引用户,并将他们吸引到应用的各个部分。
以下是使用 Microsoft Visual Studio 中的基本中心模板的自然应用的示例。

可扩展应用程序标记语言 (XAML) Hub 控件提供框架帮助你为应用更轻松地实现中心设计模式。你可以使用任何 XAML 创建视觉丰富的中心页,因此你有自定义你的应用以贴合品牌的灵活性。与从单独源显示数据的 GridView或 ListView 控件不同,每个中心分区都可显示来自不同源的数据。
你可以用于创建中心页的元素是 Hub 控件、展示磁贴、HubSection 控件以及 Hub 和 HubSection 的头。

添加中心控件
Hub 控件通常占用整个页面,而且所有页面元素(例如头)都合并到 Hub 中。将头添加到 Hub 以让用户知道它的上下文。它通常是应用的名称。你可以使用简单文本头,或者定义使用任何 XAML 内容的 HeaderTemplate。头仍然固定在其原位置,不会随中心分区滚动。
添加中心控件
- 有几种方法可将中心页添加到应用。
- 使用"中心应用"模板开始新项目。
若要开始快速创建带有中心页的新应用,请使用 Microsoft Visual Studio 2013 中的“中心应用”项目模板。(请参阅 C#、VB 和 C++ 项目模板。)
- 使用“中心页”项目模板将中心页添加到现有项目。(请参阅 C#、VB 和 C++ 项目模板。)
- 将 Hub 控件添加到现有页面。下一步显示 Page 中的 Hub 的 XAML。
- 使用"中心应用"模板开始新项目。
- 要添加简单的文本头,请将 Header 属性设置为字符串值。
<Page
x:Name="pageRoot"
x:Class="HubApp1.HubPage1"
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"
mc:Ignorable="d"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Hub Header="Field Guide"> <!-- Hub sections --> </Hub>
</Grid>
</Page> - 要将其他 XAML 元素或样式包括在头中,请将 DataTemplate 分配到 HeaderTemplate 属性。此处,头中包括后退按钮。
切记,虽然你可以在头中使用任意内容,但头的高度将影响中心分区内容中可用的垂直空间量。
注意
如果你将 Hub 控件添加到使用"基本页面"模板的页面,请将 Hub 添加到根 Grid。然后,将包含后退按钮和页面标题的 Grid 移动到 Hub.HeaderTemplate,如此处所示。
<Page
x:Name="pageRoot"
x:Class="HubApp1.HubPage1"
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"
mc:Ignorable="d"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Hub>
<Hub.HeaderTemplate>
<DataTemplate>
<!-- Back button and page title -->
<Grid Margin="0,20,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<AppBarButton x:Name="backButton" Icon="Back" Margin="-30,-14,0,0"
Click="backButton_Click"/>
<TextBlock x:Name="pageTitle" Text="Field Guide" VerticalAlignment="Top"
Style="{StaticResource HeaderTextBlockStyle}" Grid.Column="1"
IsHitTestVisible="false" TextWrapping="NoWrap"/>
</Grid>
</DataTemplate>
</Hub.HeaderTemplate> <!-- Hub sections --> </Hub>
</Grid>
</Page>
将分区添加到中心
将 Hub 的内容放在各种 HubSection 控件中。中心分区通常与应用的分区页对应。不要将内容直接添加到HubSection,而应在 DataTemplate 对象中定义 HubSection 的内容。任何有效 XAML 均可在中心分区的DataTemplate 中使用。
与 Hub 一样,HubSection 包含 Header 和 HeaderTemplate 属性,你可以使用这些属性为分区设置可选头。
将分区添加到中心
- 你可以通过添加 DataTemplate 为你的 Hub 定义 XAML 中的 HubSection 内容,如下所示。
<Hub Header="Field Guide"> <!-- Hub sections -->
<HubSection Width="500" Header="Featured">
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Image Stretch="Fill" Width="420" Height="280" Source="Assets/LightGray.png"/>
<TextBlock Style="{StaticResource SubheaderTextBlockStyle}"
Grid.Row="1" Margin="0,10,0,0" TextWrapping="Wrap"
Text="Lorem ipsum dolor sit nonumy sed consectetuer ising elit, sed diam"/>
<TextBlock Style="{StaticResource TitleTextBlockStyle}" Grid.Row="2" Margin="0,10,0,0"
Text="Description text:"/>
<TextBlock Style="{StaticResource BodyTextBlockStyle}" Grid.Row="3"
Text="Lorem ipsum dolor sit amet, consectetuer ising elit... "/>
</Grid>
</DataTemplate>
</HubSection> <!-- More sections... --> </Hub> - 要将 DataTemplate 资源用作 HubSection 的内容,请将它分配到 ContentTemplate 属性,如下:
ContentTemplate="{StaticResource FeaturedSectionTemplate}"。请参阅 ResourceDictionary 和 XAML 资源参考以获取有关使用 StaticResource 的详细信息。
<Page
x:Name="pageRoot"
x:Class="HubApp1.HubPage1"
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"
mc:Ignorable="d"> <Page.Resources>
<DataTemplate x:Key="FeaturedSectionTemplate">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Image Stretch="Fill" Width="420" Height="280" Source="Assets/LightGray.png"/>
<TextBlock Style="{StaticResource SubheaderTextBlockStyle}"
Grid.Row="1" Margin="0,10,0,0" TextWrapping="Wrap"
Text="Lorem ipsum dolor sit nonumy sed consectetuer ising elit, sed diam"/>
<TextBlock Style="{StaticResource TitleTextBlockStyle}" Grid.Row="2" Margin="0,10,0,0"
Text="Description text:"/>
<TextBlock Style="{StaticResource BodyTextBlockStyle}" Grid.Row="3"
Text="Lorem ipsum dolor sit amet, consectetuer ising elit... "/>
</Grid>
</DataTemplate>
</Page.Resources> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Hub Header="Field Guide">
<!-- Hub sections -->
<HubSection Width="500" Header="Featured" ContentTemplate="{StaticResource FeaturedSectionTemplate}"/> <!-- More Sections... -->
</Hub>
</Grid>
</Page>
添加交互式分区头用于导航
还可以将分区头设置为交互式头。通常,用户可单击交互头转到相应的应用分区页面。当其 IsHeaderInteractive属性为 true 时,默认头包括 V 型字型,以及“悬停”和“按下”视觉状态。
将分区头设置为交互式头
- 将 HubHeader.IsHeaderInteractive 属性设置为 true。
- 为 Hub.SectionHeaderClick 事件添加事件处理程序。
分区头没有单独的 Click 事件处理程序。Hub 中的每个交互式头在受到单击时引发 Hub 的SectionHeaderClick 事件。
- 添加确定哪个头受到单击并为该头执行操作的代码。通常,添加用于导航到相应的分区页的代码。
使用 HubSectionHeaderClickEventArgs 的 Section 属性确定哪个头受到单击。
此处,"特别推荐"中心分区的头为交互式头。
<Hub Header="Field Guide" SectionHeaderClick="Hub_SectionHeaderClick">
<!-- Hub sections -->
<HubSection x:Name="Featured" Width="500" Header="Featured" ContentTemplate="{StaticResource FeaturedSectionTemplate}" IsHeaderInteractive="True"/> <!-- More Sections... --> </Hub>
当头受到单击时,使用 Name 属性确定哪个头受到单击,并导航到相应的分区页。
private void Hub_SectionHeaderClick(object sender, HubSectionHeaderClickEventArgs e)
{
switch (e.Section.Name)
{
case "Featured":
this.Frame.Navigate(typeof(FeaturedPage));
break;
case "Seasonal":
this.Frame.Navigate(typeof(SeasonalPage));
break;
case "FloraAndFauna":
this.Frame.Navigate(typeof(FloraAndFaunaPage));
break;
default:
break;
}
}
将展示磁贴添加到中心
你可以使用展示图像或内容以使第一个中心分区快速吸引用户的兴趣。展示磁贴通常有覆盖屏幕的整个高度(或宽度,如果 Orientation 为 Vertical)的背景图像。
添加展示磁贴
- 将 ImageBrush 用作第一个 HubSection 的 Background。
对可水平和垂直裁剪而不丢失关注中心的展示磁贴使用大图像。你可以随意添加覆盖背景图像的内容。通常不要添加分区头,因为它会与 Hub 头冲突。
<Hub Header="Field Guide">
<!-- Hero tile -->
<HubSection Width="780" Margin="0,0,80,0">
<HubSection.Background>
<ImageBrush ImageSource="Assets/DarkGray.png" Stretch="UniformToFill"/>
</HubSection.Background>
</HubSection>
<HubSection Width="500" Header="Featured">
<!-- ... -->
</HubSection>
<!-- More sections... -->
</Hub>
使用窄应用中的垂直中心
在默认情况下,中心水平平移。如果应用支持窄视图,则可以使中心垂直平移,如下所示。

使用垂直平移的中心
- 在窄应用中,将 Orientation 属性设置为 Vertical。
<Hub Header="Field Guide" Orientation="Vertical"> <!-- Hub sections --> </Hub>
借助中心使用语义式缩放视图
如果你在 Hub 中有许多分区,请考虑添加 SemanticZoom 控件以使用户更快速地在分区间导航。有关SemanticZoom 的详细信息,请参阅语义式缩放指南。
使用具有中心的语义式缩放
- 将 Hub 添加为 SemanticZoom 控件的 ZoomedInView。
- 将 ListView(或 GridView)添加为 SemanticZoom 控件的 ZoomedOutView。
有关使用 SemanticZoom 控件的详细信息,请参阅快速入门:添加 SemanticZoom 控件。
- 向 ListView 提供应用的合适的头。在以下示例中,使页面头成为可重新使用的资源并且在 Hub 和 ListView间共享它。
由于页面头已合并到 Hub 控件中,它在用户将 SemanticZoom 更改为 ZoomedOutView 时将不可见。将头添加到 ZoomedOutView 的 ListView。
- 将 ListView 的 ItemsSource 属性绑定到 Hub 的 SectionHeaders 属性,如下所示:
ItemsSource="{Binding SectionHeaders, ElementName=FieldGuideHub}"。 - 如果 HubSection.Header 未设置为字符串值(例如,如果你使用 HeaderTemplate),则将 x:Uid 指令设置为字符串资源,如下所示:
<HubSection x:Uid="SeasonalSectionHeader" Width="520">。否则,ListView 将为该分区显示空白项。SeasonalSectionHeader的值在 resources.resw 文件中定义。请参阅快速入门:使用字符串资源获取详细信息。 - 要使分区头在 ListView 中显示,但不在 Hub 中显示,请设置 x:Uid 指令并定义空的HubSection.HeaderTemplate。
<!-- Hero tile -->
<HubSection Width="780" Margin="0,0,80,0" x:Uid="HeroTileHeader">
<HubSection.HeaderTemplate>
<DataTemplate/>
</HubSection.HeaderTemplate>
<HubSection.Background>
<ImageBrush ImageSource="Assets/DarkGray.png" Stretch="UniformToFill"/>
</HubSection.Background>
</HubSection>
以下是使用带有 Hub 的 SemanticZoom 控件的中心页和用于使用户快速导航到不同中心分区的 ListView。此 XAML 已经过简化以仅显示相关元素。例如,未显示 HubSection 内容。
<Page
x:Name="pageRoot"
x:Class="HubApp1.HubPage1"
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"
mc:Ignorable="d"> <Page.Resources>
<x:String x:Key="AppName">Field Guide</x:String> <DataTemplate x:Key="PageHeaderTemplate">
<!--Back button and page title-->
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button x:Name="backButton" Style="{StaticResource NavigationBackButtonNormalStyle}"
Margin="-1,-1,39,0"
VerticalAlignment="Top"
Command="{Binding NavigationHelper.GoBackCommand, ElementName=pageRoot}"
AutomationProperties.Name="Back"
AutomationProperties.AutomationId="BackButton"
AutomationProperties.ItemType="Navigation Button"/>
<TextBlock x:Name="pageTitle" Text="{StaticResource AppName}"
Style="{StaticResource HeaderTextBlockStyle}" Grid.Column="1"
VerticalAlignment="Top" IsHitTestVisible="false" TextWrapping="NoWrap" />
</Grid>
</DataTemplate>
</Page.Resources> <!--
This grid acts as a root panel for the page.
-->
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<SemanticZoom>
<SemanticZoom.ZoomedInView>
<Hub x:Name="FieldGuideHub" HeaderTemplate="{StaticResource PageHeaderTemplate}"
SectionHeaderClick="Hub_SectionHeaderClick"> <!-- Hero tile -->
<HubSection Width="780" Margin="0,0,80,0" x:Uid="HeroTileHeader">
<HubSection.HeaderTemplate>
<DataTemplate/>
</HubSection.HeaderTemplate>
<HubSection.Background>
<ImageBrush ImageSource="Assets/DarkGray.png" Stretch="UniformToFill"/>
</HubSection.Background>
</HubSection> <!-- Hub sections -->
<HubSection Header="Featured" Width="500">
<!-- Hub content -->
</HubSection>
<HubSection x:Uid="SeasonalSectionHeader" Width="520">
<HubSection.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="Assets/StoreLogo.png"/>
<TextBlock Text="Seasonal"/>
</StackPanel>
</DataTemplate>
</HubSection.HeaderTemplate>
<!-- Hub content -->
</HubSection>
<HubSection Header="Flora & Fauna">
<!-- Hub content -->
</HubSection>
<HubSection Header="Video">
<!-- Hub content -->
</HubSection> <!-- More sections... --> </Hub>
</SemanticZoom.ZoomedInView> <SemanticZoom.ZoomedOutView>
<ListView ItemsSource="{Binding SectionHeaders, ElementName=FieldGuideHub}"
HeaderTemplate="{StaticResource PageHeaderTemplate}"
Padding="40,60,40,0">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="Margin" Value="0,20"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>
</SemanticZoom.ZoomedOutView>
</SemanticZoom>
</Grid>
</Page>
WP8.1学习系列(第十一章)——中心控件Hub开发指南的更多相关文章
- WP8.1学习系列(第六章)——中心控件Hub面板部分交互UX
本主题中呈现的模型类似于适用于 Windows Phone 的具有主页菜单(中心或透视控件)的中心应用中描述的模型.正如之前的案例所示,你要向用户呈现不同的功能区域.此处的区别在于你可以在顶层呈现所有 ...
- WP8.1学习系列(第五章)——中心控件Hub或透视控件Pivot交互UX
具有主页菜单(中心或透视控件)的中心应用中心 你可能要设计包含许多功能的应用.当你看着这些功能时,可能会决定将它们整理到独立的区域中.这些区域最终会成为用户要访问的应用的独立部分.你需要设计一个简便的 ...
- WP8.1学习系列(第二十三章)——到控件的数据绑定
在本文中 先决条件 将控件绑定到单个项目 将控件绑定到对象的集合 通过使用数据模板显示控件中的项目 添加详细信息视图 转换数据以在控件中显示 相关主题 本主题介绍了如何在使用 C++.C# 或 Vis ...
- WP8.1学习系列(第十章)——中心控件Hub设计指南
Windows Phone 应用商店应用中的中心控件指南 在本文中 说明 示例 用法指南 设计指南 相关主题 重要的 API Hub (XAML) HubSection (XAML) 说明 中心控 ...
- WP8.1学习系列(第十二章)——全景控件Panorama开发指南
2014/6/18 适用于:Windows Phone 8 和 Windows Phone Silverlight 8.1 | Windows Phone OS 7.1 全景体验是本机 Windows ...
- WP8.1学习系列(第二十一章)——本地应用数据
了解如何存储和检索本地应用数据存储中的设置和文件. 路线图: 本主题与其他主题有何关联?请参阅: 使用 C# 或 Visual Basic 的 Windows 运行时应用的路线图 使用 C++ 的 W ...
- WP8.1学习系列(第七章)——应用选项卡Pivot交互UX
“应用选项卡”模式用于用户经常在中间导航的多个 UI 页面.如果你的应用基于单个主题(例如,电影.棒球等),该模式尤其有用.每页都将为用户显示与该应用呈现的整体数据相关的一些内容.“应用选项卡”模式可 ...
- Ext入门学习系列(五)表格控件(1)
上节学习了Ext面板控件,为后面的各个控件学习奠定基础,在此基础上本章将学习网络开发最期待的功能——表格控件. 我们都知道网络编程语言中,除了.net其他的基本没有提供网格控件,而最近的asp.net ...
- WP8.1学习系列(第十七章)——Windows Phone重要图形、视觉指示器和通知
美感在手机应用中是不可或缺的,它是直观操作的代名词.在 Windows Phone 中,你的磁贴.初始屏幕.图标.控件和导航的视觉元素会引起用户对应用程序内的相关任务.优先事项或操作的注意,并采用新颖 ...
随机推荐
- SpringMVC系列(三):REST风格
一.什么叫REST REST:即 Representational State Transfer.(资源)表现层状态转化.是目前最流行的一种互联网软件架构.它结构清晰.符合标准.易于理解.扩展方便,所 ...
- Microsoft Jet 数据库引擎打不开文件,它已经被别的用户以独占方式打开,或没有查看数据的权限。
System.Data.OleDb.OleDbException (0x80004005): Microsoft Jet 数据库引擎打不开文件'D:\wwwroot\gonghouxie\wwwroo ...
- Python之Python 安装环境搭建
方法和步骤: python安装包的下载:http://111.13.109.34:83/1Q2W3E4R5T6Y7U8I9O0P1Z2X3C4V5B/www.python.org/ftp/python ...
- Keystone-all 命令
本文档介绍Icehouse版keystone-all命令 keystone-all命令在一个进程中同时启动服务和管理API,为openstack提供服务目录,授权和身份认证服务. 用法 $ keyst ...
- James Whittaker的软件測试戒律(二)
摘录自<探索式软件測试>(注:作者模仿了圣经十诫的语气和内容编写了软件測试戒律) 1.汝应用大量输入重复锤炼汝之应用程序 2.汝应贪图汝之邻居的应用程序 3.汝应亲自寻找睿智的预言家 4. ...
- MySql折腾小记二:text/blog类型不允许设置默认值,不允许存在两个CURRENT_TIMESTAMP
From: http://www.cnblogs.com/cyq1162/archive/2011/05/17/2049055.html 在 CYQ.Data 数据框架的反向工程中,遇到MySQL的问 ...
- PHP和Apache结合 Apache默认虚拟主机
- 【主成份分析】PCA推导
### 主成份分析(Pricipal components analysis PCA) 假设空间$R^{n}$中有m个点{$x^{1},......,x^{n}$},希望压缩,对每个$x^{i}$都有 ...
- JS判断设备类型跳转至PC端或移动端相应页面
if((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobil ...
- saltstack系列1之salt-api配置与使用
salt-api salt-api是我们通过restful-api调用salt-master的接口,且调用的时候必须通过认证才能调用,认证的用户为系统用户,下面就说说如何配置salt-api. 不带S ...