WPF实现导航的几种方式

 

下面是展示的是几种导航方式:

我们来具体看下xaml文件

<Page x:Class="WPF实现Navigation.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" 
      mc:Ignorable="d" 
      d:DesignHeight="336" d:DesignWidth="626"
    Title="Page1">

    <Grid Width="619" Height="328">
        <Canvas Margin="10,53,86,37" Name="grid1">
            <Button Background="Tomato" Canvas.Left="9.26" Canvas.Top="-107" Click="button1_Click" Content="Type1" Height="48" Margin="36,113,27,0" Name="button1" VerticalAlignment="Top" Width="437" />
            <Button Background="Tomato" Canvas.Left="9" Canvas.Top="-74" Click="button2_Click" Content="Type2" Height="51" Margin="36,150,27,127" Name="button2" VerticalAlignment="Top" Width="436.74" />
            <Button Background="Tomato" Canvas.Left="9.26" Canvas.Top="155" Click="button3_Click" Content="Type3" Height="45" Margin="36,0,27,88" Name="button3" VerticalAlignment="Bottom" Width="437" />
            <TextBlock Background="Tomato" Canvas.Left="45.26" Canvas.Top="214" Height="26" Text="Type4" TextAlignment="Center" Width="437"><Hyperlink NavigateUri="Page2.xaml">超链接到 Page2</Hyperlink></TextBlock>
        </Canvas>
        <Label Content="Navigation 导航的4种方式" FontSize="24" Height="37" Margin="113,10,0,0" Name="label1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="328" />
    </Grid>
</Page>

下面我们看下具体代码展示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 WPF实现Navigation
{
    /// <summary>
    /// Page1.xaml 的交互逻辑
    /// </summary>
    public partial class Page1 : Page
    {
        public Page1()
        {
            InitializeComponent();
            this.KeepAlive = true;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //本地话操作,通过方法来跳转
            Page2 page = new Page2();
            NavigationService ns = NavigationService.GetNavigationService(this);
            ns.Navigate(page);
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            //适合游览器,通过属性的方式,uri指向
            NavigationService ns = NavigationService.GetNavigationService(this);
            ns.Source = new Uri("Page2.xaml", UriKind.Relative);
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {

            NavigationService ns = NavigationService.GetNavigationService(this);
            ns.Content = new Page2();
        }
    }
}

跳转页面展示:

具体xaml文件:

<Page x:Class="WPF实现Navigation.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" 
      mc:Ignorable="d" 
      d:DesignHeight="336" d:DesignWidth="464"
    Title="Page2">

    <Grid Height="338" Width="437"  Loaded="Grid_Loaded">
        <Canvas Height="104" Margin="36,0,37,12" Name="canvas1" VerticalAlignment="Bottom">
            <Button Canvas.Left="220" Canvas.Top="26" Click="button1_Click" Content="刷新" Height="45" HorizontalAlignment="Right" Margin="0,0,12,23" Name="button1" VerticalAlignment="Bottom" Width="76" />
            <Button Canvas.Left="-6" Canvas.Top="26" Click="button2_Click" Content="返回" Height="45" HorizontalAlignment="Left" Margin="12,0,0,23" Name="button2" VerticalAlignment="Bottom" Width="76" />
            <Button Canvas.Left="20" Canvas.Top="26" Click="button3_Click" Content="前进" Height="45" Margin="93,0,142,23" Name="button3" VerticalAlignment="Bottom" Width="76" />
        </Canvas>
        <TextBox Background="Tomato" FontSize="20" Height="71" Margin="36,124,37,0" Name="textBox1" VerticalAlignment="Top" />
    </Grid>
</Page>

后台代码实现:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 WPF实现Navigation
{
    /// <summary>
    /// Page2.xaml 的交互逻辑
    /// </summary>
    public partial class Page2 : Page
    {
     

        //过多的导航会带来性能问题,通过依赖属性来控件系统管理内存方式,进行优化
        public static DependencyProperty RAM;
        public Page2()
        {
            if (RAM == null)
            {
                this.KeepAlive = true;
                //依赖属性注册器
                Page2.RAM = DependencyProperty.Register(
                        "RAMState",
                        typeof(string),
                        typeof(Page2),
                        new FrameworkPropertyMetadata(
                        null,
                        FrameworkPropertyMetadataOptions.Journal));
            }
            InitializeComponent();
        }
        //定义一个属性访问器
        public string RAMState
        {
            get
            {
                return (string)base.GetValue(Page2.RAM);
            }
            set
            {
                base.SetValue(Page2.RAM, value);
            }
        }
        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            this.textBox1.Text = System.DateTime.Now.ToString();
        }
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            //首先判断检索导航,是否后退
            if (this.NavigationService.CanGoBack)
                this.NavigationService.GoBack();
           
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            //首先判断检索导航,是否前进
            if (this.NavigationService.CanGoForward)
                this.NavigationService.GoForward();

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //进行刷新
            this.NavigationService.Refresh();

        }
    }
}

关于导航的更多的信息,可以从msdn上了解:http://msdn.microsoft.com/zh-cn/library/vstudio/system.windows.navigation.navigationwindow(v=vs.100).aspx
简单的demo:https://files.cnblogs.com/BABLOVE/WPF%E5%AE%9E%E7%8E%B0Navigation.rar

posted @ 2013-08-03 01:39  如梦不是梦  阅读(6554)  评论(0编辑  收藏  举报