一、核心概念(必考简答题)
1. Window 与 NavigationWindow 区别
-
普通Window :只能弹出新窗口,不具备页面导航功能,无法内嵌Page页面切换。
-
NavigationWindow导航窗体 :专门用于承载 Page 页面,支持页面无刷新跳转、前进后退导航。
2. WPF层级关系
Application应用程序 → NavigationWindow导航窗口 → Page页面
一个导航窗口可以嵌套多个Page页面,实现单窗口多页面切换(网页式效果)。
二、主导航窗口 NavigationWindow(MainWindow)
核心属性考点
-
Source="xxx.xaml":窗口启动默认加载的第一个Page页面 -
ShowsNavigationUI="False":隐藏系统自带的前进/后退导航箭头
cs
<NavigationWindow x:Class="_2导航窗体.MainWindow"
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"
xmlns:local="clr-namespace:_2导航窗体"
mc:Ignorable="d"
Source="Pages/MainPage.xaml"
Title="导航主窗口" Height="450" Width="800"
ShowsNavigationUI="False">
<!-- ShowsNavigationUI="False" 隐藏系统默认导航栏箭头 -->
</NavigationWindow>
三、主页页面 MainPage.xaml(跳转入口)
知识点
-
WrapPanel:自动换行布局,控件横向排列,自动换行
-
TextDecorations:文本装饰线样式
-
Strikethrough:删除线 -
Underline:下划线
-
-
MouseLeftButtonDown:文本鼠标左键按下跳转页面
cs
<Page x:Class="_2导航窗体.Pages.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:_2导航窗体.Pages"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="主页">
<Grid>
<StackPanel>
<TextBlock FontSize="20"> 主页面 </TextBlock>
<WrapPanel Margin="20">
<!-- 删除线文本,点击跳转Page1 -->
<TextBlock Foreground="Green" FontSize="18"
MouseLeftButtonDown="TextBlock_MouseLeftButtonDown"
TextDecorations="Strikethrough">
跳转 Page1
</TextBlock>
<!-- 下划线文本,点击跳转Page2 -->
<TextBlock Margin="10,0" FontSize="18" Foreground="Green"
MouseLeftButtonDown="TextBlock_MouseLeftButtonDown_1"
TextDecorations="Underline">
跳转 Page2
</TextBlock>
</WrapPanel>
</StackPanel>
</Grid>
</Page>
四、子页面 Page1.xaml(带返回按钮)
cs
<Page x:Class="_2导航窗体.Pages.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:_2导航窗体.Pages"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="Page1">
<Grid>
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
<TextBlock FontSize="22" Margin="0,0,0,20"> Page1 页面 </TextBlock>
<Button Click="Button_Click" Width="100" Height="100" Background="Red" Foreground="White">
返回主页
</Button>
</StackPanel>
</Grid>
</Page>
五、子页面 Page2.xaml(纯展示页面)
cs
<Page x:Class="_2导航窗体.Pages.Page2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:_2导航窗体.Pages"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="Page2">
<Grid>
<TextBlock FontSize="22" VerticalAlignment="Center" HorizontalAlignment="Center">
Page2 页面
</TextBlock>
</Grid>
</Page>
六、全套后台C#导航逻辑(核心考点)
6.1 MainPage 页面跳转后台
cs
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
namespace _2导航窗体.Pages
{
public partial class MainPage : Page
{
public MainPage()
{
InitializeComponent();
}
// 跳转 Page1
private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// 导航到指定页面
this.NavigationService.Navigate(new Page1());
}
// 跳转 Page2
private void TextBlock_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
{
this.NavigationService.Navigate(new Page2());
}
}
}
6.2 Page1 返回主页后台
cs
using System.Windows;
using System.Windows.Controls;
namespace _2导航窗体.Pages
{
public partial class Page1 : Page
{
public Page1()
{
InitializeComponent();
}
// 返回上一页 / 主页
private void Button_Click(object sender, RoutedEventArgs e)
{
// 判断是否可以返回
if (this.NavigationService.CanGoBack)
{
this.NavigationService.GoBack();
}
}
}
}
七、导航核心类与方法(必背)
-
NavigationService:页面导航核心服务类
-
Navigate(new 页面名()):跳转新页面 -
GoBack():返回上一页 -
CanGoBack:判断是否存在上一页(防止报错)
八、文本装饰线枚举
-
Strikethrough:删除线
-
Underline:下划线
-
Overline:上划线
九、期末简答题汇总
1. NavigationWindow 和 Window 的区别? Window是普通窗口,无法嵌套页面;NavigationWindow是导航专用窗口,支持Page页面跳转、导航堆栈,适合单页面应用。
2. NavigationService 常用方法? Navigate跳转页面、GoBack返回上一页、CanGoBack判断是否可返回。
3. ShowsNavigationUI 属性作用? 设置为False可以隐藏系统自带的前进后退箭头,自定义页面导航样式。
4. Page和Window区别? Page无独立窗口标题栏,必须依托NavigationWindow承载;Window是独立顶层窗口。