UWP开发笔记——嵌套式页面的实现

Cle****-he UID.1073626
2016-01-28 发表

绪论

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

一种实现方法

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

在xmal中添加Frame

[mw_shl_code=csharp,true]<Frame x:Name="contentFrame" Grid.Row="1"/>[/mw_shl_code]

在code中实现子Page的navigation

[mw_shl_code=csharp,true]contentFrame.Navigate(typeof(Page1));[/mw_shl_code]

为子Frame添加默认的Page

[mw_shl_code=csharp,true]protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (e.NavigationMode == NavigationMode.New)
{
contentFrame.Navigate(typeof(Page1));
}
base.OnNavigatedTo(e);
}

[/mw_shl_code]


为back键添加Event

[mw_shl_code=csharp,true]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();
}
}
[/mw_shl_code]


一个例子

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

MainPage.xaml

[mw_shl_code=csharp,true]<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="&#xE700;" 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>
[/mw_shl_code]


MainPage.xaml.cs

[mw_shl_code=csharp,true]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 ***链接停止解析***

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;
}
}
}

[/mw_shl_code]

总结

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

开发者交流群:53078485,期待您的加入!

敬告:
为防止不可控的内容风险,本站已关闭新用户注册,新贴的发表及评论;
你现在看到的内容只是互联网用户曾经发表的言论快照,仅用于老用户留存纪念,且仅与科技行业相关,全部内容不代表本站观点及立场;
本站重新开放前已针对包括用户隐私、版权保护、信息安全、国家政策在内的各种互联网法律法规要求,执行了隐患内容的自查、屏蔽和删除;
本站目前所属个人主体,未有任何盈利安排与计划,且与原WFUN.COM所属公司不存在任何关联关系;
如果本帖内容或者相关资源侵犯到您的合法权益,或者您认为存在问题,那么请您务必点此举报或投诉!
全部回复:
caiyi000 UID.62940
2016-01-28 回复

不错。我开发的应用就是这样实现的。

本站使用Golang构建,点击此处申请开源鄂ICP备18029942号-4联系站长投诉/举报