WPF 导航

WPF 导航相关控件/机制

控件 / 类 说明 常用属性/方法
Frame 用来承载不同的页面 (Page) 并在它们之间切换的容器。 Source(导航到的 URI) Navigate()(导航方法) CanGoBack / GoBack() CanGoForward / GoForward()
Page 表示一个单独的可导航页面(可以像网页那样切换)。 NavigationService(访问导航服务)
NavigationWindow 自带导航栏的窗口,可以在多个页面(Page)间导航。 Source(启动时加载的页面) ShowsNavigationUI(是否显示导航栏)
NavigationService 后台管理 Frame 或 Page 导航动作的类。 Navigate() GoBack() GoForward() RemoveBackEntry()

常见场景示例

一、使用 Frame 控件

1. 使用 Frame 导航到一个 Page

MainWindow.xaml 中添加一个 Frame:

XML 复制代码
 <Window x:Class="WpfApp.MainWindow"
         ...>
   <Grid>
     <Frame x:Name="MainFrame" NavigationUIVisibility="Hidden" />
     <Button Content="跳转" Click="Button_Click" HorizontalAlignment="Right" VerticalAlignment="Top"/>
   </Grid>
 </Window>

MainWindow.xaml.cs 后台跳转页面:

cs 复制代码
 private void Button_Click(object sender, RoutedEventArgs e)
 {
     MainFrame.Navigate(new Uri("Page1.xaml", UriKind.Relative));
 }

2. Page 内部也可以继续导航

比如在 Page1.xaml

XML 复制代码
 <Grid>
     <Button Content="Page2" Click="GoToPage2_Click"/>
 </Grid>

Page1.xaml.cs

cs 复制代码
 private void GoToPage2_Click(object sender, RoutedEventArgs e)
 {
     NavigationService.Navigate(new Uri("Page2.xaml", UriKind.Relative));
 }

补充属性

  • NavigationUIVisibility="Hidden":隐藏 Frame 自带的导航栏。

  • JournalOwnership="OwnsJournal":让 Frame 记录自己的导航历史。

  • KeepAlive="True":Page 保持实例,不每次都重新加载(否则每次跳回来都会重新创建)。

  1. 创建一个window窗体

  2. <window> 改为<NavigationWindw >

    XML 复制代码
    <!--  NavigationWindow窗体当成其他Page页面的容器。  -->
    <!--  Source=""当前容器中应该放置哪个Page页面  -->
    <NavigationWindow
        x:Class="导航窗体.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"
        Title="MainWindow"
        Source="/Pages/MainPage.xaml"
        mc:Ignorable="d"/>
    </NavigationWindow>
  3. 把.cs 中的类继承改为 :NavigationWindow

    cs 复制代码
    public partial class MainWindow : NavigationWindow
  4. 在 xaml 中设置点鼠标击事件

  5. 在 .cs 中设置事件

    cs 复制代码
    private void TextBlock MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
      // 跳转到新页面
      this.NavigationService.Navigate(new Uri("完整的路径");
      //this.NavigationService.Navigate(new Uri(new page1);
      //this.NavigationService.Navigate(new Uri("相对路径", UriKind.Relative));
    }

UriKind :是一个枚举,它定义了如何解释 URI 字符串:

  • UriKind.Absolute:指定 URI 是一个绝对路径(完整的路径)。

  • UriKind.Relative:指定 URI 是一个相对路径(相对于当前位置)。

  • UriKind.RelativeOrAbsolute:指定 URI 可以是绝对路径或相对路径。使用这种类型,.NET 会根据输入字符串来判断 URI 是绝对的还是相对的。

C# NavigationService 属性:

成员 类型 作用
CanGoBack bool 是否可以返回上一页(是否有返回历史)。
CanGoForward bool 是否可以前进到下一页。
CurrentSource Uri 获取当前页面的地址。
BackStack IEnumerable 返回历史的集合(栈)。
ForwardStack IEnumerable 前进历史的集合(栈)
Navigate(Uri uri) 方法 导航到指定 URI 的页面。
Navigate(Object content) 方法 导航到指定的 Page 对象。
GoBack() 方法 回到上一页。
GoForward() 方法 前进到下一页。
RemoveBackEntry() 方法 删除上一页的历史记录。
Refresh() 方法 重新加载当前页
相关推荐
CoderIsArt2 小时前
三个概念:DataBinding,Dependency Property 与DataTemplate
wpf
冰茶_1 天前
WPF之Label控件详解
学习·微软·c#·wpf
baivfhpwxf20231 天前
wpf 输入框 在输入时去除水印
wpf
Dr.多喝热水1 天前
WPF App.xaml 合并资源
wpf·xaml·合并资源
未来影子1 天前
企业级分布式 MCP 方案
分布式·wpf
冰茶_1 天前
WPF之RadioButton控件详解
学习·microsoft·c#·wpf
冰茶_2 天前
WPF之Image控件详解
学习·microsoft·微软·c#·wpf·wpf控件
CoderIsArt2 天前
WPF(Windows Presentation Foundation)的内容模型
windows·wpf
冰茶_2 天前
WPF之TextBox控件详解
学习·microsoft·微软·c#·wpf