WPF Navigation

在開始學習WPF時,一開始對WPF的Window, Page, UserControl感到很迷惑。不知道何時該使用哪個。下面簡單介紹一下這三者的區別。express

Window:故名思意,桌面程序的窗體。在WPF桌面應用中,我一般會只用一個主窗體,而後將不一樣的操做單元封裝在不一樣的UserControl中,根據用戶的操做展示不一樣的UserControl;app

Page:Page須要承載在窗體中,經過Navigation進行不一樣Page的切換,也是本篇博客中須要講到的;學習

UserControl:封裝一些能夠重複使用的功能。this

在這篇博客中將介紹WPF導航應用。WPF Navigation實如今不一樣Page之間的切換。spa

咱們須要在NavigationWindow或者Frame中承載Page,首先看NavigationWindowcode

新建WelcomePage,而後設置NavigationWindow的Source爲WelcomePageorm

<NavigationWindow x:Class="NavigationBasedApp.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:NavigationBasedApp"
        mc:Ignorable="d" ShowsNavigationUI="False" Source="WelcomePage.xaml"
        Title="MainWindow" Height="350" Width="525">

</NavigationWindow>

WelcomePage.xaml:xml

<Page x:Class="NavigationBasedApp.WelcomePage"
      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:NavigationBasedApp"
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
      Title="WelcomePage">

    <Grid>
        <TextBlock Text="Hello China!" VerticalAlignment="Center" HorizontalAlignment="Center"/>
    </Grid>
</Page>

運行效果:對象

再新建一個GreetingPage.xaml,咱們在WelcomePage上添加一個Button,點擊Button時跳轉到GreetingPage.xaml,在GreetingPage上也有一個Button,點擊返回到WelcomePage。blog

WelcomePage.xaml

<Page x:Class="NavigationBasedApp.WelcomePage"
      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:NavigationBasedApp"
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
      Title="WelcomePage">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        
        <TextBlock Text="Welcome to WPF World!" HorizontalAlignment="Center"/>
        
        <Button Grid.Row="1" Width="100" Margin="0,10" Content="Go Forward" Click="GoForwardButton_Click"/>
    </Grid>
</Page>

Code Behind:

        private void GoForwardButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.NavigationService.CanGoForward)
            {
                this.NavigationService.GoForward();
            }
            else
            {
                //GreetingPage greeting = new GreetingPage();

                //this.NavigationService.Navigate(greeting);

                this.NavigationService.Navigate(new Uri("pack://application:,,,/GreetingPage.xaml"));
            }
        }

導航時,能夠傳遞GreetingPage.xaml地址,也能夠是GreetingPage對象。能夠在 if (this.NavigationService.CanGoForward) 加一個斷點,在從GreetingPage返回到WelcomePage後,再次點擊Go Forward按鈕時,直接使用this.NavigationService.GoForward()這句代碼進行了導航,這是由於導航發生後,會在NavigationService中會記錄導航記錄。

GreetingPage.xaml:

<Page x:Class="NavigationBasedApp.GreetingPage"
      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:NavigationBasedApp"
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
      Title="GreetingPage">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <TextBlock Text="Hi Yang-Fei!" HorizontalAlignment="Center"/>
        <Button Grid.Row="1" Margin="0,10" Width="100" Content="Go Back" Click="GoBackButton_Click"/>
    </Grid>
</Page>

Code Behind:

        private void GoBackButton_Click(object sender, RoutedEventArgs e)
        {
            this.NavigationService.GoBack();
        }

運行效果:

能夠在導航時傳遞參數。而後再NavigationWindow中獲取。例如:

 GreetingPage greeting = new GreetingPage();

 this.NavigationService.Navigate(greeting,"This is a test message.");

在MainWindow中,

        public MainWindow()
        {
            InitializeComponent();

            this.NavigationService.LoadCompleted += NavigationService_LoadCompleted;
        }

        private void NavigationService_LoadCompleted(object sender, NavigationEventArgs e)
        {
            if(e.ExtraData != null)
            {
               // Do something here...
            }
        }

上面提到的Frame也能夠做爲Page的承載。和NavigationWindow的區別在於NavigationWindow是全局翻頁,Frame是局部翻頁。

<Window x:Class="FrameNavigationBasedApp.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:FrameNavigationBasedApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <DockPanel>
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="_File"/>
            <MenuItem Header="_View"/>
        </Menu>
        <Frame x:Name="frmMain" NavigationUIVisibility="Hidden" Source="WelcomePage.xaml"/>
    </DockPanel>
</Window>

運行效果:

這篇博客就到這裏了,代碼點擊這裏下載。

感謝您的閱讀。

相關文章
相關標籤/搜索