C# Wpf 个人初学小案例---04PageDemo

1.实现效果:继承导航窗口NavigationWindow,实现从一个页面跳转至另一个页面(像浏览器页面那样)
2.运行结果(相关注释已写在代码中):
C# Wpf 个人初学小案例---04PageDemo_第1张图片
3.
(1)文件结构:
C# Wpf 个人初学小案例---04PageDemo_第2张图片
(2)MainWindow.xaml代码:

<NavigationWindow x:Class="CH01.PageDemo.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:CH01.PageDemo"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        Source="Page1.xaml">
NavigationWindow>

MainWindow.xaml.cs代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CH01.PageDemo
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : NavigationWindow//使继承于NavigationWindow导航窗口而不是继承于Window窗口, 因为NavigationWindow做Page界面跳转非常方便 ,可以和浏览器一样. 来回的跳转
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

(3)Page1.xaml代码:

<Page x:Class="CH01.PageDemo.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:CH01.PageDemo"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="Page1">

    <Grid>
        <TextBlock Text="This is Page 1" FontSize="20" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        <Button Content="Next" Height="30" Width="120" Margin="20" HorizontalAlignment="Right" VerticalAlignment="Bottom" Click="Button_Click"/>
    Grid>
Page>

Page1.xaml.cs代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CH01.PageDemo
{
    /// 
    /// Page1.xaml 的交互逻辑
    /// 
    public partial class Page1 : Page
    {
        public Page1()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("Page2.xaml",UriKind.Relative));//NavigationService.Navigate方法的参数为接下来要传递到的新页面;UriKind.Relative是相对的 Uri
        }
    }
}

(4)Page2.xaml代码:

<Page x:Class="CH01.PageDemo.Page2"
      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:CH01.PageDemo"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="Page2">

    <Grid>
        <Grid>
            <TextBlock Text="This is Page 2" FontSize="20" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <Button Content="Previous" Height="30" Width="120" Margin="20" HorizontalAlignment="Right" VerticalAlignment="Bottom" Click="Button_Click"/>
        Grid>
    Grid>
Page>

Page2.xaml.cs代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CH01.PageDemo
{
    /// 
    /// Page2.xaml 的交互逻辑
    /// 
    public partial class Page2 : Page
    {
        public Page2()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (NavigationService.CanGoBack)//NavigationService.CanGoBack函数自动检查是否有上一页的导航记录;返回类型为bool型,如果有前一页面记录,就返回true,否则返回false
            {
                NavigationService.GoBack();//NavigationService.GoBack函数负责跳转到前一个历史记录页面
            }
        }
    }
}

你可能感兴趣的:(wpf,wpf,c#)