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的解答 设置自定义文件格式的默认打开方式 参考链接 ...
随机推荐
- js去除空格
function trim(str){ return str.replace(/(^\s*) | ( \s*$ )/g,"" ); }
- Java整型与字符串相互转换(转)
1如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([S ...
- WPF passwordbox 圆角制作
将以下节点复制到app.xaml的<Application.Resources>节点下 <Style TargetType="PasswordBox"> ...
- SQL Server与Oracle对比学习:权限管理(一)
http://blog.csdn.net/weiwenhp/article/details/8093661 我们发现我们现在的生活中到处是涉及到密码,你要记各种各样的密码.比如银行卡,邮件,QQ,微博 ...
- Android-打反编译工具的一种方法
转载请注明出处:http://blog.csdn.net/goldenfish1919/article/details/41010261 首先我们来看下dex文件的格式: class_defs的结构: ...
- iOS上文本绘制的几种方法
文本绘制在开发客户端程序中是一个比较常用的功能,可分为采用控件和直接绘制两种方式. 采用控件的方式比较简便,添加一个比如UILabel对象,然后设置相关属性就好了.但这种方式局限性也比较大. 直接绘制 ...
- Linux块设备驱动 --块驱动相关的结构体及相关操作
http://blog.chinaunix.net/uid-23399063-id-70124.html
- SDWebImage 原理及使用
这个类库提供一个UIImageView类别以支持加载来自网络的远程图片.具有缓存管理.异步下载.同一个URL下载次数控制和优化等特征. SDWebImage 加载图片的流程 入口 setImageWi ...
- linux中的帮助命令 分类: linux 学习笔记 ubuntu 2015-07-05 19:07 31人阅读 评论(0) 收藏
说实话,到目前为止我还是不太习惯使用linux自带的帮助文档,遇到问题都是去查我自己下载的chm格式的命令大全,不过这些帮助命令我们还是有必要了解的. 1.man [要查看的命令名称] 例如想要查看l ...
- Android(java)学习笔记176:BroadcastReceiver之 短信发送的广播接收者
有时候,我们需要开发出来一个短信监听器,监听用户发送的短信记录,下面就是一个案例,这里同样需要使用广播机制. 下面同样是代码示例,MainActivity.java 和 activity_main. ...