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

我们来具体看下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. js 设置cookie

    function GetCookieVal(offset) // 获得Cookie解码后的值 { var endstr = document.cookie.indexOf(";", ...

  2. PAT---1005. Spell It Right (20)

    #include<iostream> #include<stack> #include<string.h> ]= {"zero", " ...

  3. ARCproject中加入非ARC文件,或者非ARC环境中加入ARC文件

    ARC与非ARC在一个项目中同一时候使用, 选择项目中的Targets,选中你所要操作的Target,选Build Phases,在当中Complie Sources中选择须要ARC的文件双击,并在输 ...

  4. [PHP] find ascii code in string

    if (strpos($data ,chr(0x95)) !== false) { echo 'true'; }else{ echo "false"; }

  5. labview中层叠式顺序结构与平铺式顺序结构有什么不同?

    也就看着不同,平铺式看着直观,但比较占地方,程序复杂了就显得过大.二者可互相转换,从这点也可以看出它们没有本质不同!

  6. Android ListView实现仿iPhone实现左滑删除按钮

    需要自定义ListView.这里就交FloatDelListView吧. 复写onTouchEvent方法.如下: @Override public boolean onTouchEvent(Moti ...

  7. php 的一个pg_fetch_assoc的怪问题

    遇到过一种问题 . if($row=pg_fetch_assoc($result)){ while($row=pg_fetch_assoc($result)){ echo '3333'; $koCd ...

  8. Canvas保存图片保存到本地

    使用Canvas绘图,将图片保存到本地方法 一.使用HTML5 a标签的download属性,将图片保存到本地,不需要链接服务器 关于download属性:HTML5 <a>标签downl ...

  9. 对于EditText的详细用法

    EditText这个控件对于每一个Android开发者来说都是再熟悉不过了,但是,为什么有的人的EditText可以表现的那么好看,而刚入学Android的程序员来讲却丑到爆.这就充分的说明对于Edi ...

  10. C#当中的多线程_线程基础

    前言 最近工作不是很忙,想把买了很久了的<C#多线程编程实战>看完,所以索性把每一章的重点记录一下,方便以后回忆. 第1章 线程基础 1.创建一个线程 using System; usin ...