NavigationWindow类

NavigationWindow 是 WPF 中专门用于承载 Page 页面、自带完整浏览器式导航功能的顶层窗口控件。
 
你可以把它理解成:自带前进 / 后退 / 刷新 / 停止导航栏的 Window,专门用来做多页面导航应用(向导、设置中心、帮助文档、流程表单)。

一、核心定位

1. 是什么?

  • 继承自 Window → 是一个真正的顶层窗口,可独立运行
  • 内置 导航历史栈(Journal)
  • 内置 导航工具栏(前进、后退、停止、刷新)
  • 专门用来显示 Page,不能直接放按钮 / 文本框

2. 与 Window / Frame / Page 的关系

  • Window:普通顶层窗口,无导航
  • NavigationWindow:带导航功能的 Window
  • Frame:嵌入在窗口里的 “小导航容器”
  • Page:只能放在 NavigationWindow 或 Frame 中显示

3. 适用场景

  • 向导式界面(安装向导、注册流程)
  • 帮助文档
  • 设置中心
  • 多步骤表单
  • 类浏览器的内容浏览应用

二、核心功能

  1. 自动显示导航栏(前进 / 后退 / 停止 / 刷新)
  2. 自动管理导航历史(不用自己写栈)
  3. 自动显示 Page.Title 到窗口标题
  4. 支持导航事件(跳转前、跳转后、加载完成)
  5. 支持页面缓存
  6. 完全兼容 Page 的所有导航功能

 三、常用关键属性

属性作用
Source 设置默认显示的 Page 路径(如 Source="HomePage.xaml")
CanGoBack 是否可以后退(只读)
CanGoForward 是否可以前进(只读)
ShowsNavigationUI 是否显示系统自带导航栏(默认 True)
JournalOwnership 导航历史归属(默认自动管理)
Title 窗口标题(会被 Page.Title 覆盖)
NavigationService 导航服务对象,代码控制跳转

四、核心导航方法

 1 // 跳转到页面
 2 Navigate(new Page1());
 3 Navigate(new Uri("Page1.xaml", UriKind.Relative));
 4 
 5 // 后退
 6 GoBack();
 7 
 8 // 前进
 9 GoForward();
10 
11 // 停止加载
12 StopLoading();
13 
14 // 刷新当前页
15 Refresh();
16 
17 // 关闭窗口
18 Close();

五、导航生命周期(最重要)

1 Navigating → 跳转前(可拦截)
2 Navigated → 跳转完成
3 LoadCompleted → 页面加载完毕

对应重写方法(Page 中):

1 OnNavigatedTo()    // 进入页面
2 OnNavigatedFrom()  // 离开页面

六、完整实战示例(可直接运行)

步骤 1:创建 WPF 项目

步骤 2:添加 3 个 Page

  • HomePage.xaml
  • Step1Page.xaml
  • Step2Page.xaml

步骤 3:创建 NavigationWindow(作为主窗口)

1. NavigationWindow.xaml

 1 <NavigationWindow x:Class="WpfNavWindowDemo.MainNavWindow"
 2                   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3                   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4                   Title="导航窗口演示"
 5                   Height="450"
 6                   Width="650"
 7                   WindowStartupLocation="CenterScreen"
 8                   ShowsNavigationUI="True"  <!-- 显示导航栏 -->
 9                   Source="HomePage.xaml">   <!-- 默认页面 -->
10 </NavigationWindow>

2. 修改 App 启动窗口

App.xaml
1 StartupUri="MainNavWindow.xaml"

页面示例

3. HomePage.xaml(首页)

 1 <Page x:Class="WpfNavWindowDemo.HomePage"
 2       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4       Title="首页">
 1 using System.Windows;
 2 using System.Windows.Controls;
 3 
 4 namespace WpfNavWindowDemo
 5 {
 6     public partial class HomePage : Page
 7     {
 8         public HomePage()
 9         {
10             InitializeComponent();
11         }
12 
13         private void Button_Click(object sender, RoutedEventArgs e)
14         {
15             // 跳转到 Step1Page
16             this.NavigationService.Navigate(new Step1Page());
17         }
18 
19         // 进入页面时触发
20         protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
21         {
22             base.OnNavigatedTo(e);
23             MessageBox.Show("欢迎来到首页!");
24         }
25     }
26 }

 

 5 
 6     <Grid Background="LightBlue">
 7         <TextBlock Text="首页" FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Center"/>
 8         <Button Content="进入步骤1" Click="Button_Click" 
 9                 Width="160" Height="40" VerticalAlignment="Bottom" Margin="30"/>
10     </Grid>
11 </Page>

HomePage.xaml.cs

 1 using System.Windows;
 2 using System.Windows.Controls;
 3 
 4 namespace WpfNavWindowDemo
 5 {
 6     public partial class HomePage : Page
 7     {
 8         public HomePage()
 9         {
10             InitializeComponent();
11         }
12 
13         private void Button_Click(object sender, RoutedEventArgs e)
14         {
15             // 跳转到 Step1Page
16             this.NavigationService.Navigate(new Step1Page());
17         }
18 
19         // 进入页面时触发
20         protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
21         {
22             base.OnNavigatedTo(e);
23             MessageBox.Show("欢迎来到首页!");
24         }
25     }
26 }

4. Step1Page.xaml(步骤 1)

 1 <Page x:Class="WpfNavWindowDemo.Step1Page"
 2       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4       Title="步骤1">
 5 
 6     <Grid Background="LightGreen">
 7         <TextBlock Text="步骤1" FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Center"/>
 8         <Button Content="下一步" Click="Button_Click" 
 9                 Width="160" Height="40" VerticalAlignment="Bottom" Margin="30"/>
10     </Grid>
11 </Page>

Step1Page.xaml.cs

 1 using System.Windows;
 2 using System.Windows.Controls;
 3 
 4 namespace WpfNavWindowDemo
 5 {
 6     public partial class Step1Page : Page
 7     {
 8         public Step1Page()
 9         {
10             InitializeComponent();
11         }
12 
13         private void Button_Click(object sender, RoutedEventArgs e)
14         {
15             this.NavigationService.Navigate(new Step2Page());
16         }
17 
18         // 离开页面时触发
19         protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
20         {
21             base.OnNavigatedFrom(e);
22             MessageBox.Show("即将离开步骤1");
23         }
24     }
25 }

5. Step2Page.xaml(步骤 2)

1 <Page x:Class="WpfNavWindowDemo.Step2Page"
2       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4       Title="步骤2">
5 
6     <Grid Background="LightCoral">
7         <TextBlock Text="步骤2(完成)" FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Center"/>
8     </Grid>
9 </Page>

七、超实用高级技巧

1. 隐藏系统导航栏

1 ShowsNavigationUI="False"

2. 页面缓存(返回不刷新)

1 <Page KeepAlive="True">

3. 拦截跳转(防止误操作)

1 protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
2 {
3     base.OnNavigatingFrom(e);
4     if (MessageBox.Show("确定离开?", "提示", MessageBoxButton.YesNo) == MessageBoxResult.No)
5     {
6         e.Cancel = true;
7     }
8 }

4. 代码控制 NavigationWindow

1 // 获取当前窗口
2 var navWin = Application.Current.MainWindow as NavigationWindow;
3 
4 navWin.Navigate(new Page1());
5 navWin.GoBack();
6 navWin.Close();

八、NavigationWindow 与 Frame 的区别

类型作用
NavigationWindow 顶层窗口,全屏导航
Frame 嵌入在窗口 / 控件中的局部导航
 
简单规则:
  • 整个窗口都是导航页面 → 用 NavigationWindow
  • 窗口一部分区域导航 → 用 Frame

九、总结(最核心)

  1. NavigationWindow = Window + 内置导航栏 + 历史记录
  2. 必须用来显示 Page
  3. 自动管理前进 / 后退
  4. 生命周期:OnNavigatedTo / OnNavigatedFrom
  5. 适合:向导、流程、设置、帮助文档
  6. 可隐藏自带导航栏,自定义 UI
posted on 2026-03-27 14:13  工业搬砖猿Lee  阅读(0)  评论(0)    收藏  举报