?????????以下是一個簡單的WPF示例,演示如何在三個Page之間進行導航切換,使用Frame
控件作為導航容器,并包含基本的導航按鈕(前進/后退/主頁)
? ? ? ? Page類更簡單,比Window更精簡。
? ? ? ? 代碼見下文以及資源文件:
?https://download.csdn.net/download/qq_34047402/90919296
5WPF中的Page頁面的使用資源-CSDN文庫
?本例介紹如下界面實現:
一、Page的顯示
Page可以放到Frame中。
1), Frame的? NavigationUIVisibility ="Visible"?可以顯示導航的小圖標。
?<Frame x:Name="MainFrame" Grid.Column="1" NavigationUIVisibility="Visible"/>
2).使用Frame的Navigate方法可以導航到某個頁面,如下文
? MainFrame.Navigate(new Page1());?
3)頁面之間跳轉可以使用超級鏈接或者NavigationService.Navigate實現,
a).?NavigationService
????????NavigationService.Navigate(new Page3());
????????其中NavigationService:獲得了頁的宿
b). 使用Frame的GoBack,GoForward函數
?if (MainFrame.CanGoBack)
? ? ?MainFrame.GoBack();
if (MainFrame.CanGoForward)
? ? MainFrame.GoForward();
c). 超級鏈接
?<TextBlock Margin="10">
? ? ?click <Hyperlink NavigateUri="Page3.xaml" > 這兒</Hyperlink> 到頁面3
?</TextBlock>
如果想對URi做驗證,可以添加事件RequestNavigate 來處理具體的導航請求。
?<TextBlock Margin="10">
? ? ?click <Hyperlink NavigateUri="Page3.xaml" ?RequestNavigate="Hyperlink_RequestNavigate"> 這兒</Hyperlink> 到頁面3
?</TextBlock>
?private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
?{
? ? ?if (e.Uri != null)
? ? ?{
? ? ? ? ?// 獲取當前NavigationService并導航
? ? ? ? ?var navService = NavigationService.GetNavigationService((DependencyObject)sender);
? ? ? ? ?navService?.Navigate(new Uri(e.Uri.ToString(), UriKind.RelativeOrAbsolute));
? ? ? ? ?e.Handled = true;? ?//設置為?true
?表示事件已處理
? ? ?}
?}
關于參數?RequestNavigateEventArgs:
屬性/方法 | 說明 |
---|---|
e.Uri | 獲取Hyperlink中指定的目標URI(如?NavigateUri="Page2.xaml" ) |
e.Handled | 設置為?true ?表示事件已處理,阻止默認行為(必須設置!) |
e.Source | 事件源(即Hyperlink控件本身) |
二、代碼如下
<Window x:Class="WpfApp1.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:WpfApp1"mc:Ignorable="d"Title="Page導航示例" Height="450" Width="800"><Grid><Grid.ColumnDefinitions><ColumnDefinition Width="Auto"/><ColumnDefinition Width="*"/></Grid.ColumnDefinitions><!-- 導航按鈕 --><StackPanel Grid.Row="0" Orientation="Vertical" Background="LightGray"><Button Content="首頁" Margin="5" Padding="10,2" Click="GoToHome_Click"/><Button Content="上一頁" Margin="5" Padding="10,2" Click="GoBack_Click"/><Button Content="下一頁" Margin="5" Padding="10,2" Click="GoForward_Click"/><Button Content="用Command跳轉到頁面2" Margin="5" Padding="10,2" Command="{Binding NavigationToPageCommand}" CommandParameter="/Pages/Page2.xaml" /><Button Content="用Command跳轉到頁面3" Margin="5" Padding="10,2" Command="{Binding NavigationToPageCommand}" CommandParameter="/Pages/Page3.xaml" /><TextBlock Margin="10" VerticalAlignment="Center" Text="{Binding ElementName=MainFrame, Path=Content.Title}"/></StackPanel><!-- 導航容器 --><Frame x:Name="MainFrame" Grid.Column="1" NavigationUIVisibility="Hidden"/></Grid>
</Window>
<Page x:Class="WpfApp1.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:WpfApp1.Pages"mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"Title="頁面1歡迎"><StackPanel VerticalAlignment="Center" HorizontalAlignment="Center"><TextBlock Text="這是頁面1" FontSize="24" Margin="10"/><Button Content="前往頁面2" Click="NavigateToPage2" Width="100" Margin="10"/></StackPanel>
</Page>
public partial class Page1 : Page{public Page1(){InitializeComponent();}private void NavigateToPage2(object sender, RoutedEventArgs e){NavigationService.Navigate(new Page2());}}
public partial class Page2 : Page
{public Page2(){InitializeComponent();}private void NavigateToPage3(object sender, RoutedEventArgs e){NavigationService.Navigate(new Page3());}private void GoBackToPage1(object sender, RoutedEventArgs e){NavigationService.GoBack();}
}public partial class Page3 : Page{public Page3(){InitializeComponent();}private void GoToHome(object sender, RoutedEventArgs e){// 直接導航到Page1(清空導航歷史)NavigationService.Navigate(new Page1());}}public partial class MainWindow : Window{public MainWindow(){InitializeComponent();this.DataContext = new MainViewModel(this.MainFrame);}private void GoToHome_Click(object sender, RoutedEventArgs e){MainFrame.Navigate(new Page1());}private void GoBack_Click(object sender, RoutedEventArgs e){if (MainFrame.CanGoBack)MainFrame.GoBack();}private void GoForward_Click(object sender, RoutedEventArgs e){if (MainFrame.CanGoForward)MainFrame.GoForward();}}public class MainViewModel{private Frame _frame;public MainViewModel(Frame frame){_frame = frame;}private ICommand _navigationToPageCommand;public ICommand NavigationToPageCommand {get{return _navigationToPageCommand ?? new RelayCommand<object>( NavigationToPage, (s) => true);}set{_navigationToPageCommand = value;}}private void NavigationToPage(object page){try{string pageUrl = page as string;if(pageUrl!=null){_frame.Navigate(new Uri(pageUrl, UriKind.Relative));}}catch(Exception ex){}}}