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="" 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实现导航的几种方式的更多相关文章
- WPF设置样式的几种方式
第一种方式是直接使用Setter来进行,可以对Background等进行设置. <Window.Resources> <Style TargetType="Button&q ...
- WPF实现动画的几种方式及其小案例
WPF实现动画的方式: 基于计时器的动画 建立一个定时器,然后根据其频率循环调用函数或者一个事件处理函数,在这个函数中可以手工更新目标属性,直到达到最终值,这时可以停止计时器. 案例: 效果图: XA ...
- iOS 滑动隐藏导航栏-三种方式
/** 1隐藏导航栏-简单- */ self.navigationController.hidesBarsOnSwipe = YES; /** 2隐藏导航栏-不随tableView滑动消失效果 ...
- WPF 应用 - WPF 播放 GIF 的两种方式
1. 使用 Winform 的 PictureBox 1.1 引用 dll WindowsFormsIntegration.dll System.Windows.Forms.dll System.Dr ...
- WPFの三种方式实现快捷键
最近,对wpf添加快捷键的方式进行了整理.主要用到的三种方式如下: 一.wpf命令: 资源中添加命令 <Window.Resources> <RoutedUICommand x:Ke ...
- MVC3+EF4.1学习系列(五)----- EF查找导航属性的几种方式
文章索引和简介 通过上一篇的学习 我们把demo的各种关系终于搭建里起来 以及处理好了如何映射到数据库等问题 但是 只是搭建好了关系 问题还远没有解决 这篇就来写如何查找导航属性 和查找导航属性的几种 ...
- WPF中使用文件浏览对话框的几种方式
原文:WPF中使用文件浏览对话框的几种方式 WPF本身并没有为我们提供文件浏览的控件, 也不能直接使用Forms中的控件,而文件浏览对话框又是我们最常用的控件之一. 下面是我实现的方式 方式1: 使用 ...
- WPF中实现PropertyGrid(用于展示对象的详细信息)的三种方式
原文:WPF中实现PropertyGrid(用于展示对象的详细信息)的三种方式 由于WPF中没有提供PropertyGrid控件,有些业务需要此类的控件.这篇文章介绍在WPF中实现PropertyGr ...
- WPF -- 使用当前进程打开自定义文件的一种方式
问题描述 当双击打开自定义格式的文件时,希望使用当前正在运行的进程,而不是另起一个进程. 本文介绍一种方式解决如上问题,方案参考user3582780的解答 设置自定义文件格式的默认打开方式 参考链接 ...
随机推荐
- CodeForces - 269C Flawed Flow
http://codeforces.com/problemset/problem/269/C 题目大意: 给定一个边没有定向的无法增广的残量网络且1是源点,n是汇点,给定每条边中的流. 让你把所有边 ...
- TreeMap简单simple
TreeMap能够按照主键对里面的数据进行排序,基于上篇文章:java集合类之TreeMap中有关于TreeMap内部实现的详细介绍.本文主要是写了些使用TreeMap的简单demo. 要想实现Tre ...
- jQuery之动画效果show()......animate()
jQuery之动画效果 1.show()显示效果 语法:show(speed,callback) Number/String,Function speend为动画执行时间,单位为毫秒.也可以为slow ...
- .jar是什么文件?(转载)
JAR(Java ARchive,Java 归档)是一种与平台无关的文件格式,可将多个文件合成一个文件.用户可将多个 Java applet 及其所需组件(.class 文件.图像和声音)绑定到 JA ...
- Ueditor自定义默认宽度高度
如题. 最近需要使用到网页后台富文本编辑器.经过同学推荐,最后决定使用百度家的Ueditor. 官方地址:http://ueditor.baidu.com/website/ 贴吧地址:ueditor讨 ...
- Lamda和Linq语法对比详细
本人转载:http://www.cnblogs.com/knowledgesea/p/3897665.html 闲言碎语 近期比较忙,但还是想写点什么,就分享一些基础的知识给大家看吧,希望能帮助一些l ...
- android 双卡手机发短信/判断手机是否为双卡
通过反射发现sendMultipartTextMessage多了一个phoneType的参数,这个参数就是这只用GMS还是CDMA卡发送的. phoneType获取方法,iTelephony.getA ...
- mybatis04 根据用户名称模糊查询用户信息
根据用户名称模糊查询用户信息可能返回多条记录. 1.1.1User.xml 编码 如果用%进行模糊查询,#{}表示一个占位符会被翻译为一个?号(SELECT * FROM USER WHERE id= ...
- myeclipse自动生成可持久化类的映射文件的方法
1.打开DB Browser,新建一个数据库的连接,找到想要持久化操作的数据库表的图标,右键选择hibernate reverse engineering 2.之后出现如下所示: java src f ...
- linux 安装软件的地方
用下边这个命令:mysqladmin -u root -p variables root是你的数据库帐号回车后会提示你输入密码,输入上边填写的帐号对应的密码 回车后出来一个大表,找到datadir这一 ...