WPF 支援 **瀏覽器式導航應用** (XBAP) 與 **獨立導航應用** (NavigationWindow)。本文修正書中過時語法 (NavigationApplication → Application),展示 Page 導航、Hyperlink 聲明式跳轉、NavigationService 程式化控制。
<Application x:Class="MyNavApp.App"
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Page1.xaml"> <!-- 指定啟動頁面 -->
<Application.Resources />
</Application>
<Page
x:Class="MyNavApp.Page1"
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
Title="Page 1"> <!-- Page 用 Title 非 Text -->
<TextBlock FontSize="72" TextWrapping="Wrap"> <!-- TextWrapping 非 TextWrap -->
Check out
<Hyperlink NavigateUri="Page2.xaml">page 2</Hyperlink>, <!-- 核心:聲明式跳轉 -->
too.
</TextBlock>
</Page>
<!-- XAML 聲明式 -->
<Hyperlink NavigateUri="Page2.xaml">Go Page 2</Hyperlink>
<!-- 等價後台代碼 -->
NavigationService.GetNavigationService(this).Navigate(new Uri("Page2.xaml", UriKind.Relative));
<Page ...>
<StackPanel>
<Button Content="上一頁" Click="Back_Click" />
<Button Content="下一頁" Click="Forward_Click" />
<Button Content="跳轉 Page3" Click="Jump_Click" />
</StackPanel>
</Page>
// Code-behind
void Back_Click(object sender, RoutedEventArgs e) {
NavigationService.GetNavigationService(this).GoBack();
}
void Forward_Click(...) { ...GoForward(); }
void Jump_Click(...) { ...Navigate(new Uri("Page3.xaml", UriKind.Relative)); }
| 舊版 (書中) | 新版 (當前) |
|---|---|
| `NavigationApplication` | `Application` (通用) |
| `TextBlock.TextWrap` | `TextBlock.TextWrapping` |
| `Page.Text` | `Page.Title` |
WPF 導航框架已較少用於新專案。現代替代:
- **MVVM + Frame/Page** (單頁應用風格)
- **Prism/Caliburn.Micro** 區域導航
- **Avalonia/Uno Platform** 跨平台導航
但理解 `NavigationService`/`Journal` 機制,對掌握 WPF 視圖生命週期、狀態管理仍有參考價值。