本文参考了官方文档以及提供的示例代码(官方代码貌似有点误导,所以写了这一篇,并且文末有GayHub代码地址)

官方文档发布于20180806,说明NavigationView刚发布了没几天,还在开发中,所以里面的接口啥的随时会变! 随时会变! 随时会变! ,用在你的项目之前,请需要做好充足的心理准备。

不过他变任他变,效果还是非常诱人的。看一下效果图

2018 新版 NavigationView实现了汉堡菜单和顶部菜单的近乎完美结合,可以参考官方商店的设计风格,就是用的这个控件。

不过要用这个控件,也不是那么容易的,因为他需要Windows UI Library支持,而这个库不支持VS2015,必须是2017或者更高版本。详见  Getting started with the Windows UI Library

下面就说一下实现的完整步骤吧。

1.安装Microsoft.UI.Xaml

在NuGet中搜索Microsoft.UI.Xaml,需要勾选预发行版本。

然后需要将Microsoft.UI.Xaml添加到应用程序资源。

a. 如果你程序没有其他的程序资源,那么

<Application>
<Application.Resources>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls"/>
</Application.Resources>
</Application>

b. 如果已存在其他的,那么只需要合并一下即可

<Application>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>

2. 添加引用

在xaml文件中,添加

xmlns:controls="using:Microsoft.UI.Xaml.Controls"

在.cs文件中添加

using MUXC = Microsoft.UI.Xaml.Controls;

3. 编写Xaml界面

注意一下被注释掉的代码,你可以反注释一下,看看效果。

我这里主要是模仿一下商店的风格。

代码和官方的不大一样,主要是控件NavigationView,官方并没有在前面加上命名空间controls,我这里加上了。如果不加的话,编译器并不知道i用的是新版的控件。后台代码如果不加的话,会爆出N个错误。

详见我的Issue Developers and VS may get confused with namespace Microsoft.UI.Xaml.Controls and Windows.UI.Xaml.Controls

<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState>
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="{x:Bind NavView.CompactModeThresholdWidth}" />
</VisualState.StateTriggers> <VisualState.Setters>
<Setter Target="NavView.PaneDisplayMode" Value="Top"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups> <controls:NavigationView x:Name="NavView"
SelectionFollowsFocus="Enabled"
ItemInvoked="NavView_ItemInvoked"
IsSettingsVisible="True"
Loaded="NavView_Loaded"
BackRequested="NavView_BackRequested">
<controls:NavigationView.MenuItems>
<controls:NavigationViewItem Content="Home" x:Name="home" Tag="home">
<controls:NavigationViewItem.Icon>
<FontIcon Glyph=""/>
</controls:NavigationViewItem.Icon>
</controls:NavigationViewItem>
<!--<controls:NavigationViewItemSeparator/>-->
<!--<controls:NavigationViewItemHeader Content="Main pages"/>-->
<controls:NavigationViewItem Icon="AllApps" Content="Apps" x:Name="apps" Tag="apps"/>
<controls:NavigationViewItem Icon="People" Content="Games" x:Name="games" Tag="games"/>
<controls:NavigationViewItem Icon="Video" Content="Movies and TVs" x:Name="moviestvs" Tag="moviestvs"/>
<controls:NavigationViewItem Icon="Audio" Content="Music" x:Name="music" Tag="music"/>
<controls:NavigationViewItem Icon="PhoneBook" Content="Books" x:Name="books" Tag="books"/>
</controls:NavigationView.MenuItems> <!--<controls:NavigationView.AutoSuggestBox>
<AutoSuggestBox x:Name="ASB" QueryIcon="Find"/>
</controls:NavigationView.AutoSuggestBox>--> <!--<controls:NavigationView.HeaderTemplate>
<DataTemplate>
<Grid Margin="24,10,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Style="{StaticResource TitleTextBlockStyle}"
FontSize="28"
VerticalAlignment="Center"
Text="Welcome"/>
<CommandBar Grid.Column="1"
HorizontalAlignment="Right"
VerticalAlignment="Top"
DefaultLabelPosition="Right"
Background="{ThemeResource SystemControlBackgroundAltHighBrush}">
<AppBarButton Label="Refresh" Icon="Refresh"/>
<AppBarButton Label="Import" Icon="Import"/>
</CommandBar>
</Grid>
</DataTemplate>
</controls:NavigationView.HeaderTemplate>--> <Frame x:Name="ContentFrame"/>
</controls:NavigationView> <TextBlock Text="New Navigation View" FontSize="12" Margin="12,6,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"/>
</Grid>

4. 添加后台代码

首先添加一下必要的引用

using System;
using System.Collections.Generic;
using System.Linq;
using Windows.Foundation;
using Windows.System;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Navigation;
using MUXC = Microsoft.UI.Xaml.Controls;

红色代码部分是我自己添加的,官方没有。

因为发现当初与设置界面的时候,拖动窗体改变大小,app会崩溃。目前已经提交官方pr,等待审核

        private Type currentPage;
// List of ValueTuple holding the Navigation Tag and the relative Navigation Page
private readonly IList<(string Tag, Type Page)> _pages = new List<(string Tag, Type Page)>
{
("home", typeof(HomePage)),
("apps", typeof(AppsPage)),
("games", typeof(GamesPage)),
("moviestvs", typeof(MovieandTVPage)),
("music", typeof(MusicPage)),
("books", typeof(BooksPage)),
}; private void NavView_Loaded(object sender, RoutedEventArgs e)
{
// You can also add items in code behind
//NavView.MenuItems.Add(new NavigationViewItemSeparator());
//NavView.MenuItems.Add(new NavigationViewItem
//{
// Content = "Settings",
// Icon = new SymbolIcon(Symbol.Folder),
// Tag = "content"
//});
//_pages.Add(("content", typeof(SettingsPage))); ContentFrame.Navigated += On_Navigated; // NavView doesn't load any page by default: you need to specify it
NavView_Navigate("home"); // Add keyboard accelerators for backwards navigation
var goBack = new KeyboardAccelerator { Key = VirtualKey.GoBack };
goBack.Invoked += BackInvoked;
this.KeyboardAccelerators.Add(goBack); // ALT routes here
var altLeft = new KeyboardAccelerator
{
Key = VirtualKey.Left,
Modifiers = VirtualKeyModifiers.Menu
};
altLeft.Invoked += BackInvoked;
this.KeyboardAccelerators.Add(altLeft);
} private void NavView_ItemInvoked(MUXC.NavigationView sender, MUXC.NavigationViewItemInvokedEventArgs args)
{
if (args.InvokedItem == null)
return; if (args.IsSettingsInvoked)
ContentFrame.Navigate(typeof(SettingsPage));
else
{
// Getting the Tag from Content (args.InvokedItem is the content of NavigationViewItem)
var navItemTag = NavView.MenuItems
.OfType<MUXC.NavigationViewItem>()
.First(i => args.InvokedItem.Equals(i.Content))
.Tag.ToString(); NavView_Navigate(navItemTag);
}
} private void NavView_Navigate(string navItemTag)
{
var item = _pages.First(p => p.Tag.Equals(navItemTag));
if (currentPage == item.Page)
return;
ContentFrame.Navigate(item.Page); currentPage = item.Page;
} private void NavView_BackRequested(MUXC.NavigationView sender, MUXC.NavigationViewBackRequestedEventArgs args)
{
On_BackRequested();
} private void BackInvoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args)
{
On_BackRequested();
args.Handled = true;
} private bool On_BackRequested()
{
if (!ContentFrame.CanGoBack)
return false; // Don't go back if the nav pane is overlayed
if (NavView.IsPaneOpen &&
(NavView.DisplayMode == MUXC.NavigationViewDisplayMode.Compact ||
NavView.DisplayMode == MUXC.NavigationViewDisplayMode.Minimal))
return false; ContentFrame.GoBack();
return true;
} private void On_Navigated(object sender, NavigationEventArgs e)
{
NavView.IsBackEnabled = ContentFrame.CanGoBack; if (ContentFrame.SourcePageType == typeof(SettingsPage))
{
// SettingsItem is not part of NavView.MenuItems, and doesn't have a Tag
NavView.SelectedItem = (MUXC.NavigationViewItem)NavView.SettingsItem;
}
else
{
var item = _pages.First(p => p.Page == e.SourcePageType); NavView.SelectedItem = NavView.MenuItems
.OfType<MUXC.NavigationViewItem>()
.First(n => n.Tag.Equals(item.Tag));
}
}

如果你发现,代码中

IList<(string Tag, Type Page)> _pages = new List<(string Tag, Type Page)>

报错,请在NuGet中安装 System.ValueTuple

5. 延申扩展

商店还有一个效果,就是ScrollViewer向下拉的时候,再导航栏下面会有一个模糊的玻璃效果。

这个效果官方也做了说明,参考

Scroll content under top pane

不过我的程序中没做上去,因为 ScrollViewer的  CanContentRenderOutsideBounds  这个属性,再17134中并没有,应该在 Insider 17723出现了。

详见

Windows 10 SDK Preview Build 17723 available now! JULY 31, 2018 10:36 AM

但是不晓得这个属性,会不会照顾低版本系统呢。。。。。。又是一个坑!!!

我在HomePage里面注释了,预览版可以尝试一下。

<ScrollViewer>
<!--<ScrollViewer CanContentRenderOutsideBounds"True">-->
<StackPanel>
<TextBlock FontSize="" TextWrapping="Wrap" Margin="0,100,0,0" IsColorFontEnabled="True" HorizontalAlignment="Center" VerticalAlignment="Center">
<Run>For a seamless look+feel, if your app has pages that use a ScrollViewer and your navigation pane is top positioned, we recommend having the content scroll underneath the top nav pane.
This can be achieved by setting the CanContentRenderOutsideBounds property on the relevant ScrollViewer to true.</Run>
</TextBlock>
<TextBlock Text="Home" FontSize="" Margin="0,100,0,0" IsColorFontEnabled="True" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<TextBlock Text="Home" FontSize="" Margin="0,100,0,0" IsColorFontEnabled="True" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<TextBlock Text="Home" FontSize="" Margin="0,100,0,0" IsColorFontEnabled="True" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<TextBlock Text="Home" FontSize="" Margin="0,100,0,0" IsColorFontEnabled="True" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<TextBlock Text="Home" FontSize="" Margin="0,100,0,0" IsColorFontEnabled="True" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<TextBlock Text="Home" FontSize="" Margin="0,100,0,0" IsColorFontEnabled="True" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</StackPanel>
</ScrollViewer>

6. 再延申扩展一下

官方还说 Microsoft.UI.Xaml 支持更低的系统版本,最低支持到14393。但是在编译程序的时候,目标系统一定要是17134或者更高。

有低版本的系统,你可以在低版本上跑一下看看。反正半个月前是不能运行的,我测试过。

不过你的实际程序可能还有其他的官方库,那些有的可能最低是16299,所以14393也只是一个水中月而已。

7. GayHub项目地址

https://github.com/hupo376787/NewNavigationView

参考官方文档:

Windows UI Library (Preview version)

Navigation view (Preview version)

Windows 10 SDK Preview Build 17723 available now! JULY 31, 2018 10:36 AM

UWP 2018 新版 NavigationView 尝鲜的更多相关文章

  1. CabloyJS 4.12震撼发布,及新版教程尝鲜

    引言 凡是可以用 JavaScript 来写的应用,最终都会用 JavaScript 来写 | Atwood 定律 目前市面上出现的大多数与 NodeJS 相关的框架,基本都将 NodeJS 定位在工 ...

  2. 从[Greenplum 6.0] 1分钟安装尝鲜开始

    Greenplum目前6版本目前已经迭代了几个小版本了,随着版本的更新,不断的有bug被修复. 打算试用的朋友可以入手了. 作为开年的第一个工作日的第一个帖子,必须从“开天辟地”的6.0开始.以下内容 ...

  3. Java协程编程之Loom项目尝鲜

    前提 之前很长一段时间关注JDK协程库的开发进度,但是前一段时间比较忙很少去查看OpenJDK官网的内容.Java协程项目Loom(因为项目还在开发阶段,OpenJDK给出的官网https://ope ...

  4. React Suspense 尝鲜,处理前后端IO异步操作

    简单介绍一下Suspense Suspense主要用来解决网络IO问题,它早在2018年的React 16.6.0版本中就已发布.它的相关用法有些已经比较成熟,有的相对不太稳定,甚至经历了重命名.删除 ...

  5. Windows 10 周年版尝鲜

    早在今年的 Build 大会上,微软就开始宣传最新的 Windows 10 周年版更新,炫了不少特技,直到昨天(2016/8/2 PST)才正式放出,相关新闻可以参考这里,正式的版本为 Version ...

  6. 【翻译】五步快速使用LINQPad尝鲜StreamInsight

    StreamInsight  学习地址:http://www.cnblogs.com/StreamInsight/archive/2011/10/26/StreamInsight-Query-Seri ...

  7. 小程序新能力-个人开发者尝鲜微信小程序

    个人开发者的福利 微信小程序,刚听到这个新名词的时候,我就兴冲冲的去找入口,看看自己能不能搞个微信小程序的HelloWorld,毕竟能在微信上把自己写的一些小工具跑起来还是满炫酷的. 没想,网上一查, ...

  8. 【响应式】foundation栅格布局的“尝鲜”与“填坑”

      提到响应式,就不得不提两个响应式框架--bootstrap和foundation.在标题上我已经说明白啦,今天给大家介绍的是foundation框架. 何为"尝鲜"?就是带大伙 ...

  9. Spring-Data-JPA尝鲜:快速搭建CRUD+分页后台实例

    前言:由于之前没有接触过Hibernate框架,但是最近看一些博客深深被它的"效率"所吸引,所以这就来跟大家一起就着一个简单的例子来尝尝Spring全家桶里自带的JPA的鲜 Spr ...

随机推荐

  1. Two Sum LT1

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  2. 初步认识linux的top命令

    今天学习了一下top命令,强大无比啊! top命令涉及到的东西很多.用来监视系统的运行状态,top打印包括cpu.内存.进程使用情况的统计信息,还打印出进程列表. 输入top命令,不带任何参数,默认打 ...

  3. 13个开源GIS软件 你了解几个?

    地理信息系统(Geographic Information System,GIS)软件依赖于覆盖整个地球的数据集.为处理大量的 GIS 数据及其格式,编程人员创建了若干开源库和 GIS 套件. GIS ...

  4. vue组件实现查看大图效果

    使用的index.vue代码 <template> <img :src="imgUrl" @click="clickImg($event)"& ...

  5. hdu-1059(多重背包+二进制优化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1059 题意:输入6个数,每个数ni代表价值为i的物品有ni个.求如果这些物品能均分给两个人,每个人获得 ...

  6. .NET性能优化(文摘)

    第1章 性能指标 1.1 性能目标 1.2 性能指标 第2章 性能度量 2.1 性能度量方式 白盒测试-小程序 黑盒测试-大型程序 2.2 Windows内置工具 2.2.1 性能计数器 2.2.2  ...

  7. java Concurrent包学习笔记(一):ExecutorService

    一.介绍 ExecutorService是java.util.concurrent包中的一个线程池实现接口.其有两个实现类: 1)ThreadPoolExecutor:普通线程池通过配置线程池大小,能 ...

  8. s4-7 生成树协议

    Spanning Tree :为了可靠,采用冗余结构:但是透明网桥 会产生无休止循环的问题 冗余交换拓扑可能带来的问题  广播风暴  多帧传送  MAC地址库不稳定 生成树协议  STP:sp ...

  9. vs2013查看代码性能后删除保存的性能查看文件导致再打开提示未找到与约束匹配的导出

    1.关闭VS: 2.删除C:/Users//AppData/Local/Microsoft/VisualStudio/12.0/ComponentModelCache文件夹下所有文件及文件夹: 3.重 ...

  10. printk()、查看开机log、查看实时log

    要将linux内核的带级别控制的printk内容打印出来,在命令行 输入 dmesg -n 8 就将所有级别的信息都打印出来 Linux命令:dmesg 功能说明:显示开机信息. 语 法:dmesg ...