绪论

UWP开发中,Page是最常用的Control之一,通常情况下,在开发的application中,每一个页面就是一个Page。有时候,为了开发整合度更高,UI表现更为一致的UI,开发者需要把UI控件和功能整合到一个页面的子页面中,子页面拥有自己的UI表现和生命周期,这就需要在Page中嵌套Page来达到需要实现的效果。

一种实现方法

其实,实现嵌套页面是一件很简单的事情,我们知道,page都是通过Frame显示和控制Navigation的,基于这点,就可以在主页面(即最外层的页面)中添加一个Frame,通过控制这个Frame来实现子Page的显示和导航。

在xmal中添加Frame

<Frame x:Name="contentFrame" Grid.Row="1"/>

在code中实现子Page的navigation

contentFrame.Navigate(typeof(Page1));

为子Frame添加默认的Page

protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (e.NavigationMode == NavigationMode.New)
{
contentFrame.Navigate(typeof(Page1));
}
base.OnNavigatedTo(e);
}

为back键添加Event

public MainPage()
{
this.InitializeComponent();
SystemNavigationManager.GetForCurrentView().BackRequested += PageBackRequested;
} private void PageBackRequested(object sender, BackRequestedEventArgs e)
{
if (contentFrame == null)
return;
if (contentFrame.CanGoBack)
{
e.Handled = true;
contentFrame.GoBack();
}
}

一个例子

在这个例子中,外层的MainPage有一个汉堡键配合SplitView菜单实现内层Page的切换,back键用来实现contentFrame的Navigation。其中,Page1和Page2是嵌套在MainPage里面的两个Page。

MainPage.xaml

<Page
x:Class="NestedFrameExample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:NestedFrameExample"
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}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Height="48" VerticalAlignment="Top" Background="White">
<Button x:Name="hamburgerButton" Background="Transparent" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Left" Click="SplitViewToggle_Click">
<TextBlock Text="" FontFamily="{ThemeResource SymbolThemeFontFamily}"/>
</Button>
</Grid>
<!-- 这个Frame用于显示子Page页面。 -->
<Frame x:Name="contentFrame" Grid.Row="1"/> <SplitView x:Name="splitView" Grid.Row="1" IsPaneOpen="False" OpenPaneLength="300" CompactPaneLength="48" DisplayMode="Overlay" HorizontalContentAlignment="Stretch">
<SplitView.Pane>
<ListView ItemsSource="{x:Bind Path=NavLinks}" IsItemClickEnabled="True" ItemClick="NavLinkClick" SelectionMode="None"/>
</SplitView.Pane>
</SplitView>
</Grid>
</Page>

MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation; // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 namespace NestedFrameExample
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{ public List<NavLink> NavLinks = new List<NavLink>()
{
new NavLink() { Label = "Page1", LinkType = typeof(Page1) },
new NavLink() { Label = "Page2", LinkType = typeof(Page2) }
}; public MainPage()
{
this.InitializeComponent();
SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
SystemNavigationManager.GetForCurrentView().BackRequested += PageBackRequested;
} private void NavLinkClick(object sender, ItemClickEventArgs e)
{
NavLink link = e.ClickedItem as NavLink;
if (link != null && link.LinkType != null)
contentFrame.Navigate(link.LinkType);
splitView.IsPaneOpen = false;
} private void SplitViewToggle_Click(object sender, RoutedEventArgs e)
{
splitView.IsPaneOpen = !splitView.IsPaneOpen;
} protected override void OnNavigatedTo(NavigationEventArgs e)
{
//this.InitialBackButton();
if (e.NavigationMode == NavigationMode.New)
{
contentFrame.Navigate(typeof(Page1));
}
base.OnNavigatedTo(e);
} private void PageBackRequested(object sender, BackRequestedEventArgs e)
{
if (contentFrame == null)
return;
if (contentFrame.CanGoBack)
{
e.Handled = true;
contentFrame.GoBack();
}
}
} public class NavLink
{
public String Label { get; set; }
public Type LinkType { get; set; }
public override String ToString()
{
return Label;
}
}
}

总结

嵌套式的页面使每个Page的结构更加清晰,更能专注于自己的功能实现,也使代码更加清晰,容易维护,避免代码冗余,推荐使用,希望本文能给大家带来帮助!!

UWP开发笔记——嵌套式页面的实现的更多相关文章

  1. UWP开发---通过委托跨页面导航

    -前言 做过.Net开发的都了解,当二级窗口操作主窗口的控件时通常用委托的方式.那么在UWP开发中,常常会遇到MainPage的二级Frame里面的内容去操作MainPage的导航跳转,具体看下图: ...

  2. 张高兴的 UWP 开发笔记:横向 ListView

    ListView 默认的排列方向是纵向 ( Orientation="Vertical" ) ,但如果我们需要横向显示的 ListView 怎么办? Blend for Visua ...

  3. APPCAN开发笔记:html页面之间的参数传递:使用js获取url中的参数,以及在APPCAN中不能使用的解决方法

    用PHP的GET/POST方式来传递方式已经是司空见惯了,但是如果我的页面是一个静态的html的页面,想传递参数的时候要怎么办呢?在APPCAN的开发中我们会经常遇到这样的问题,因为所有的页面都是静态 ...

  4. 张高兴的 UWP 开发笔记:用 Thumb 控件仿制一个可拖动 Button

    在 WPF 上可用的控件拖动方法在 UWP 上大多没用,那干脆用 Thumb 仿制一个吧. 关于 Thumb 控件的教程也不多,毕竟在 WPF 控件拖动有很多种方法, Thumb 就显得很鸡肋了.下面 ...

  5. 张高兴的 UWP 开发笔记:应用内启动应用 (UWP Launch UWP)

    需求:在 A 应用内启动 B 应用,如果 B 应用未安装则跳转应用商店搜索. 启动方式使用 Uri 启动,本文使用尽可能简单,并且能拿来直接用的代码.不涉及启动后的应用数据交互,如需深入了解,请戳 M ...

  6. 张高兴的 UWP 开发笔记:汉堡菜单进阶

    不同于Windows 8应用,Windows 10引入了"汉堡菜单"这一导航模式.说具体点,就拿官方的天气应用来说,左上角三条横杠的图标外加一个SplitView控件组成的这一导航 ...

  7. 张高兴的 UWP 开发笔记:手机状态栏 StatusBar

    UWP 有关应用标题栏 TitleBar 的文章比较多,但介绍 StatusBar 的却没几篇,在这里随便写写.状态栏 StatusBar 用法比较简单,花点心思稍微设计一下,对应用会是个很好的点缀. ...

  8. 张高兴的 UWP 开发笔记:定制 ContentDialog 样式

    我需要一个背景透明的 ContentDialog,像下图一样.如何定制?写了一个简单的示例(https://github.com/ZhangGaoxing/uwp-demo/tree/master/C ...

  9. 张高兴的 Xamarin.Forms 开发笔记:为 Android 与 iOS 引入 UWP 风格的汉堡菜单 ( MasterDetailPage )

    所谓 UWP 样式的汉堡菜单,我曾在"张高兴的 UWP 开发笔记:汉堡菜单进阶"里说过,也就是使用 Segoe MDL2 Assets 字体作为左侧 Icon,并且左侧使用填充颜色 ...

随机推荐

  1. XE3随笔18:实例 - 解析 Google 关键字搜索排名

    同上例类似, 通过 'http://clients1.google.cn/complete/search?&q=' + "关键字" 可以获取 Google 的关键字搜索排名 ...

  2. jsp文件引入js文件的方式(项目部署于web容器中)

    在页面中引入javascript文件的方式是多种多样的,本文介绍两种. 通过<script>标签插入js文件 通过这种方式引入的js,写对js文件和jsp文件的路径很重要.下面给出一个项目 ...

  3. c# - catch(Exception ex) 会丢掉StackTrace 是怎么回事?

    原本这篇文章就想写写StackTrace怎么会丢的问题, 但现在的内容变成了讨论怎么处理Exception的问题. 该不该用try catch, 什么时候用?也困扰了我很久, 好像随便写写就可以, 但 ...

  4. redis的 rdb 和 aof 持久化的区别 [转]

    aof,rdb是两种 redis持久化的机制.用于crash后,redis的恢复. rdb的特性如下: Code: fork一个进程,遍历hash table,利用copy on write,把整个d ...

  5. arcgis操作笔记-根据属性提取某区域要素

    1. 提取

  6. SQL 2005 服务器更计算机名

    select @@ServerNamesp_dropserver '288-BHGKF0MF5G6' sp_addserver '288VIP124','LOCAL' select serverpro ...

  7. Windows结构化异常

    不错的总结: http://hi.baidu.com/wangxvfeng101/item/518f6efdab4e5616ff35820e http://www.vckbase.com/index. ...

  8. cnavas

    1.创建一个画布eg:<cnavas width=100px;height=100px; style="border:1px solid rgba(242 ,54,33,0.1)&qu ...

  9. Spark难道比oracle性能还差?百万级数据测试性能

    本人对大数据方面也是刚刚研究,由于工作需要在实时查询与统计的性能方面要深入学习.现测试性能如下:   环境:VirtualBox  host-only ubuntu版本: Linux master 4 ...

  10. Swift基础语法(二)

    操作符 swift中运算符基本基础于c,下面我就给大家白话白话那些被优化过的运算符们 //加号减号的妙用 var a= var b= -a//此时b=-1 var c = +b//此时 c=-1 va ...