WPF導(dǎo)航在page中的實現(xiàn)方法
WPF中的有許多功能還需要我們在實際開發(fā)中去慢慢的體會其用法,以此來總結(jié)自己的使用技巧。在這里我們就先來了解下WPF導(dǎo)航的一些實現(xiàn)方法。#t#
首先WPF導(dǎo)航在Page頁中加一個< Hyperlink>的標簽,再添中一個Click事件,指向后臺處理程序。如下:
- < Hyperlink Click="
hyperlink_Click">你好< /Hyperlink>
在.cs文件中,需要引入 using System.Windows.Navigation命名空間
然后處理事件,也就是WPF導(dǎo)航主體了。
- void hyperlink_Click
(object sender, Routed
EventArgs args) - {
- }
有四種WPF導(dǎo)航方法(事例中是由當前頁向Page4導(dǎo)航),如下:
1、
- Page4 page = new Page4();
- NavigationService ns =
NavigationService.GetNavi
gationService(this);- ns.Navigate(page);
2、
- NavigationService ns =
NavigationService.GetNavi
gationService(this);- ns.Source = new Uri
("Page4.xaml", UriKind.
Relative);
3、
- NavigationService ns =
NavigationService.GetNavi
gationService(this);- ns.Content = new Page4();
4、
- Page4 page = new
Page4();- this.NavigationService.
Navigate(page);
5、
- this.Navigation
Service.Refresh();//導(dǎo)航到本頁
如果想緩存瀏覽過的頁面可以在Page頭中設(shè)置:
- < Page
- x:Class="BrowserApp"
- xmlns="http://schemas.microso
ft.com/winfx/2006/xaml/
presentation"- xmlns:x="http://schemas.
microsoft.com/winfx/2006/xaml"- WindowTitle="Page3"
- KeepAlive="True">
- < /Page>
如果想減少內(nèi)存的開銷,可以使用這個WPF導(dǎo)航方法(注意文中黑體字):
- public static readonly
DependencyProperty RetainedStateDP;- using System;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Navigation;
- public partial class RetainedState
DPPage : System.Windows.Controls.Page- {
- public static readonly
DependencyProperty RetainedStateDP;- static RetainedStateDPPage()
- {
- RetainedStateDPPage.RetainedStateDP =
- DependencyProperty.Register(
- "RetainedState",
- typeof(string),
- typeof(RetainedStateDPPage),
- new FrameworkPropertyMetadata(
- null,
- FrameworkPropertyMetadata
Options.Journal));- }
- public RetainedStateDPPage()
- {
- InitializeComponent();
- }
- public string RetainedState
- {
- get
- {
- return (string)base.GetValue
(RetainedStateDPPage.RetainedStateDP);- }
- set
- {
- base.SetValue(RetainedStateDPPage.
RetainedStateDP, value);- }
- }
- }