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

我们来具体看下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="" d:DesignWidth=""
Title="Page1"> <Grid Width="" Height="">
<Canvas Margin="10,53,86,37" Name="grid1">
<Button Background="Tomato" Canvas.Left="9.26" Canvas.Top="-107" Click="button1_Click" Content="Type1" Height="" Margin="36,113,27,0" Name="button1" VerticalAlignment="Top" Width="" />
<Button Background="Tomato" Canvas.Left="" Canvas.Top="-74" Click="button2_Click" Content="Type2" Height="" Margin="36,150,27,127" Name="button2" VerticalAlignment="Top" Width="436.74" />
<Button Background="Tomato" Canvas.Left="9.26" Canvas.Top="" Click="button3_Click" Content="Type3" Height="" Margin="36,0,27,88" Name="button3" VerticalAlignment="Bottom" Width="" />
<TextBlock Background="Tomato" Canvas.Left="45.26" Canvas.Top="" Height="" Text="Type4" TextAlignment="Center" Width=""><Hyperlink NavigateUri="Page2.xaml">超链接到 Page2</Hyperlink></TextBlock>
</Canvas>
<Label Content="Navigation 导航的4种方式" FontSize="" Height="" Margin="113,10,0,0" Name="label1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="" />
</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="" d:DesignWidth=""
Title="Page2"> <Grid Height="" Width="" Loaded="Grid_Loaded">
<Canvas Height="" Margin="36,0,37,12" Name="canvas1" VerticalAlignment="Bottom">
<Button Canvas.Left="" Canvas.Top="" Click="button1_Click" Content="刷新" Height="" HorizontalAlignment="Right" Margin="0,0,12,23" Name="button1" VerticalAlignment="Bottom" Width="" />
<Button Canvas.Left="-6" Canvas.Top="" Click="button2_Click" Content="返回" Height="" HorizontalAlignment="Left" Margin="12,0,0,23" Name="button2" VerticalAlignment="Bottom" Width="" />
<Button Canvas.Left="" Canvas.Top="" Click="button3_Click" Content="前进" Height="" Margin="93,0,142,23" Name="button3" VerticalAlignment="Bottom" Width="" />
</Canvas>
<TextBox Background="Tomato" FontSize="" Height="" 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:http://files.cnblogs.com/BABLOVE/WPF%E5%AE%9E%E7%8E%B0Navigation.rar

WPF实现导航的几种方式的更多相关文章

  1. WPF设置样式的几种方式

    第一种方式是直接使用Setter来进行,可以对Background等进行设置. <Window.Resources> <Style TargetType="Button&q ...

  2. WPF实现动画的几种方式及其小案例

    WPF实现动画的方式: 基于计时器的动画 建立一个定时器,然后根据其频率循环调用函数或者一个事件处理函数,在这个函数中可以手工更新目标属性,直到达到最终值,这时可以停止计时器. 案例: 效果图: XA ...

  3. iOS 滑动隐藏导航栏-三种方式

    /** 1隐藏导航栏-简单- */    self.navigationController.hidesBarsOnSwipe = YES; /** 2隐藏导航栏-不随tableView滑动消失效果 ...

  4. WPF 应用 - WPF 播放 GIF 的两种方式

    1. 使用 Winform 的 PictureBox 1.1 引用 dll WindowsFormsIntegration.dll System.Windows.Forms.dll System.Dr ...

  5. WPFの三种方式实现快捷键

    最近,对wpf添加快捷键的方式进行了整理.主要用到的三种方式如下: 一.wpf命令: 资源中添加命令 <Window.Resources> <RoutedUICommand x:Ke ...

  6. MVC3+EF4.1学习系列(五)----- EF查找导航属性的几种方式

    文章索引和简介 通过上一篇的学习 我们把demo的各种关系终于搭建里起来 以及处理好了如何映射到数据库等问题 但是 只是搭建好了关系 问题还远没有解决 这篇就来写如何查找导航属性 和查找导航属性的几种 ...

  7. WPF中使用文件浏览对话框的几种方式

    原文:WPF中使用文件浏览对话框的几种方式 WPF本身并没有为我们提供文件浏览的控件, 也不能直接使用Forms中的控件,而文件浏览对话框又是我们最常用的控件之一. 下面是我实现的方式 方式1: 使用 ...

  8. WPF中实现PropertyGrid(用于展示对象的详细信息)的三种方式

    原文:WPF中实现PropertyGrid(用于展示对象的详细信息)的三种方式 由于WPF中没有提供PropertyGrid控件,有些业务需要此类的控件.这篇文章介绍在WPF中实现PropertyGr ...

  9. WPF -- 使用当前进程打开自定义文件的一种方式

    问题描述 当双击打开自定义格式的文件时,希望使用当前正在运行的进程,而不是另起一个进程. 本文介绍一种方式解决如上问题,方案参考user3582780的解答 设置自定义文件格式的默认打开方式 参考链接 ...

随机推荐

  1. Android Studio 1.0.2 设置内存大小

    http://www.linuxidc.com/Linux/2015-04/116457.htm Android studio 1.0.2默认最大内存是750M,这样跑起来非常的卡,难以忍受,机器又不 ...

  2. java基础入门-arraylist存储开销

    今天我们来看一下arraylist的存储开销,由于在项目其中,我尝试了一个很大的arraylist.然后内存爆了 所以我看了下源代码.原来arraylist的存储开销是比較大的,先上代码 import ...

  3. \n 与 \r

    符号 ASCII码 意义 \n 换行NL \r 回车CR 回车 \r 本义是光标重新回到本行开头,r的英文return,控制字符可以写成CR,即Carriage Return 换行 \n 本义是光标往 ...

  4. 为什么java不支持多重继承?

    什么是钻石问题?如下图所示,B和C继承于A,D继承B和C(多重继承),在D中调用A的方法时,无法判断是调用B中的实现还是C的实现,下图继承关系是个菱形,所以该问题又叫做菱形问题. 如果java要解决这 ...

  5. iOS 网络编程:socket

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  6. SAMBA用户访问指定的目录

    指定某个用户访问一个特定的共享文件夹sfx 用户可以访问abc目录 别的用户不可以访问abc目录 先创建一个用户命令useradd sfx 创建一个smbpasswd用户 在创建这个用户时要先创建一个 ...

  7. 学习java随笔第六篇:数组

    一维数组 创建一维数组并输出 public class OneDimensionalArray { public static void main(String argas[]) { int i[]= ...

  8. javascript 动态操作Html

    <html> <body> <p>aaaaa</p> <input type="button" value="con ...

  9. Git查看、删除、重命名远程分支和tag

    这篇文章记录我在使用git的过程中碰到远程分支和tag的相关内容,提纲: 查看远程分支 删除远程分支和tag 删除不存在对应远程分支的本地分支 重命名远程分支 把本地tag推送到远程 获取远程tag ...

  10. objective-c中字符串长度计算

    我们知道,在c语言中,使用sizeof ()计算在内存中占用的字节数, 引用string.h后,使用strlen()计算字符串的长度(不包含\0). 而在object-c中, "length ...