WPF導航在page中的實現方法
作者:佚名
WPF導航的實現方法有很多種。我們在文章中通過各種代碼示例為大家詳細介紹了這些方法的具體操作步驟,希望對大家有所幫助。
WPF中的有許多功能還需要我們在實際開發中去慢慢的體會其用法,以此來總結自己的使用技巧。在這里我們就先來了解下WPF導航的一些實現方法。#t#
首先WPF導航在Page頁中加一個< Hyperlink>的標簽,再添中一個Click事件,指向后臺處理程序。如下:
- < Hyperlink Click="
hyperlink_Click">你好< /Hyperlink>
在.cs文件中,需要引入 using System.Windows.Navigation命名空間
然后處理事件,也就是WPF導航主體了。
- void hyperlink_Click
(object sender, Routed
EventArgs args) - {
- }
有四種WPF導航方法(事例中是由當前頁向Page4導航),如下:
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();//導航到本頁
如果想緩存瀏覽過的頁面可以在Page頭中設置:
- < 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>
如果想減少內存的開銷,可以使用這個WPF導航方法(注意文中黑體字):
- 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);- }
- }
- }
責任編輯:曹凱
來源:
博客園