C#WPF如何跳转页面

1. 创建导航窗口(NavigationWindow)

NavigationWindow 是专门用于页面导航的窗口,自带导航按钮(前进 / 后退)。

步骤 1:新建 NavigationWindow

在项目中添加一个NavigationWindow(如MainNavWindow.xaml):

复制代码
<!-- MainNavWindow.xaml -->
<NavigationWindow x:Class="WpfApp.MainNavWindow"
                  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  Title="导航窗口" Width="800" Height="600"
                  Source="Page1.xaml"> <!-- 设置初始页面 -->
</NavigationWindow>

步骤 2:设置启动窗口

App.xaml中指定启动窗口为MainNavWindow

复制代码
<Application x:Class="WpfApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainNavWindow.xaml"> <!-- 改为导航窗口 -->
</Application>
2. 创建 Page 页面

新建多个Page(如Page1.xamlPage2.xaml)作为跳转目标:

Page1.xaml(包含跳转到 Page2 的按钮):

复制代码
<Page x:Class="WpfApp.Page1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Page1">
    <StackPanel>
        <TextBlock FontSize="20" Text="这是页面1"/>
        <Button Content="跳转到页面2" Click="GoToPage2"/>
    </StackPanel>
</Page>

Page1.xaml.cs(实现跳转逻辑):

复制代码
private void GoToPage2(object sender, RoutedEventArgs e) {
    // 跳转到Page2
    this.NavigationService.Navigate(new Page2());
}
3. 常用导航操作
  • 返回上一页:

    复制代码
    if (this.NavigationService.CanGoBack) {
        this.NavigationService.GoBack(); // 后退
    }
  • 前进到下一页:

    复制代码
    if (this.NavigationService.CanGoForward) {
        this.NavigationService.GoForward(); // 前进
    }
  • 刷新当前页:

    复制代码
    this.NavigationService.Refresh();

二、在 Window 中切换 UserControl(适合单窗口应用)

如果不需要导航历史,可在Window中通过切换UserControl实现 "页面" 切换,更灵活。

1. 创建 UserControl(模拟页面)

新建两个UserControl(如UC_Home.xamlUC_Settings.xaml):

UC_Home.xaml

复制代码
<UserControl x:Class="WpfApp.UC_Home"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <TextBlock FontSize="20" Text="这是首页"/>
</UserControl>

UC_Settings.xaml

复制代码
<UserControl x:Class="WpfApp.UC_Settings"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <TextBlock FontSize="20" Text="这是设置页"/>
</UserControl>
2. 在 Window 中切换 UserControl

在主窗口中用一个容器(如Grid)承载UserControl,通过代码动态切换:

MainWindow.xaml

复制代码
<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="切换页面示例" Width="800" Height="600">
    <Grid>
        <!-- 导航按钮 -->
        <StackPanel Orientation="Horizontal" Margin="10">
            <Button Content="首页" Click="ShowHome" Margin="5"/>
            <Button Content="设置" Click="ShowSettings" Margin="5"/>
        </StackPanel>
        
        <!-- 页面容器(用于显示UserControl) -->
        <Grid x:Name="ContentContainer" Margin="10,50,10,10"/>
    </Grid>
</Window>

MainWindow.xaml.cs(切换逻辑):

复制代码
private void ShowHome(object sender, RoutedEventArgs e) {
    // 清空容器,添加首页UserControl
    ContentContainer.Children.Clear();
    ContentContainer.Children.Add(new UC_Home());
}
​
private void ShowSettings(object sender, RoutedEventArgs e) {
    // 清空容器,添加设置页UserControl
    ContentContainer.Children.Clear();
    ContentContainer.Children.Add(new UC_Settings());
}

三、两种方式的对比

方式 优点 缺点 适用场景
NavigationWindow+Page 自带导航历史(前进 / 后退) 导航栏样式固定,定制性差 类似浏览器的多页面导航
Window+UserControl 完全自定义布局,灵活度高 需手动实现导航历史(如需要) 单窗口应用,如管理系统界面

根据需求选择合适的方式,小型应用推荐用Window+UserControl,需要完整导航功能时用NavigationWindow+Page

相关推荐
一轮弯弯的明月20 小时前
贝尔数求集合划分方案总数
java·笔记·蓝桥杯·学习心得
sg_knight21 小时前
设计模式实战:状态模式(State)
python·ui·设计模式·状态模式·state
航Hang*21 小时前
第3章:Linux系统安全管理——第2节:部署代理服务
linux·运维·服务器·开发语言·笔记·系统安全
周杰伦fans21 小时前
C# required 关键字详解
开发语言·网络·c#
黄思搏21 小时前
基于标注平台数据的 Unity UI 自动化构建工作流设计与工程实践
ui·unity·蓝湖·vectoui
zjnlswd21 小时前
tkinter学习案例--笔记代码
笔记·学习
独小乐1 天前
009.中断实践之实现按键测试|千篇笔记实现嵌入式全栈/裸机篇
linux·c语言·驱动开发·笔记·嵌入式硬件·arm
无聊大侠hello world1 天前
Yu-AI-Agent 项目(AI 恋爱大师智能体) · 学习笔记
人工智能·笔记·学习
CheerWWW1 天前
C++学习笔记——箭头运算符、std::vector的使用、静态链接、动态链接
c++·笔记·学习
ZhiqianXia1 天前
Pytorch 学习笔记(17):decompositions.py —— 算子分解的百科全书
pytorch·笔记·学习