Share via


CustomContentState 类

定义

使用 CustomContentState,可以在源内容片段的不同状态之间导航,而无需为每个后续导航重新加载源内容。

public ref class CustomContentState abstract
[System.Serializable]
public abstract class CustomContentState
public abstract class CustomContentState
[<System.Serializable>]
type CustomContentState = class
type CustomContentState = class
Public MustInherit Class CustomContentState
继承
CustomContentState
属性

示例

下面是替代 JournalEntryNameCustomContentState实现示例。

using System;
using System.Windows.Controls;
using System.Windows.Navigation;
[Serializable]
public class MyCustomContentState : CustomContentState
{
    string dateCreated;
    TextBlock dateTextBlock;

    public MyCustomContentState(string dateCreated, TextBlock dateTextBlock)
    {
        this.dateCreated = dateCreated;
        this.dateTextBlock = dateTextBlock;
    }

    public override string JournalEntryName
    {
        get
        {
            return "Journal Entry " + this.dateCreated;
        }
    }

    public override void Replay(NavigationService navigationService, NavigationMode mode)
    {
        this.dateTextBlock.Text = this.dateCreated;
    }
}

Imports System.Windows.Controls
Imports System.Windows.Navigation
<Serializable>
Public Class MyCustomContentState
    Inherits CustomContentState
    Private dateCreated As String
    Private dateTextBlock As TextBlock

    Public Sub New(ByVal dateCreated As String, ByVal dateTextBlock As TextBlock)
        Me.dateCreated = dateCreated
        Me.dateTextBlock = dateTextBlock
    End Sub

    Public Overrides ReadOnly Property JournalEntryName() As String
        Get
            Return "Journal Entry " & Me.dateCreated
        End Get
    End Property

    Public Overrides Sub Replay(ByVal navigationService As NavigationService, ByVal mode As NavigationMode)
        Me.dateTextBlock.Text = Me.dateCreated
    End Sub
End Class

注解

默认情况下, NavigationService 不会在导航历史记录中存储内容对象的实例。 NavigationService而是在每次使用导航历史记录导航到内容对象时创建一个新实例。 此行为旨在避免在导航到大量内容和大量内容时过度消耗内存。 因此,从一个导航到另一个导航,内容的状态不会被记住。 但是,WPF 提供了将一段自定义状态与一段内容的导航历史记录条目相关联的功能。

与导航历史记录条目关联的自定义状态必须是派生自 CustomContentState的类。 使用以下方法之一 CustomContentState 将 对象与导航历史记录项相关联:

注意

如果调用 AddBackEntry 方法,则必须处理 Navigation 事件或实现 IProvideCustomContentState

导航历史记录条目导航到时,WPF 会检查自定义对象是否 CustomContentState 与之关联。 如果是这样,它将调用 Replay 以允许自定义 CustomContentState 对象应用它从上一个导航中记住的状态。

自定义 CustomContentState 类可以重写 JournalEntryName 以更改对象关联到的导航历史记录条目 CustomContentState 显示的名称。 返回的值 JournalEntryName 在各种导航器的导航 UI 中可见, (浏览器、 NavigationWindowFrame) 。

派生自 CustomContentState 的类必须是可序列化的,这意味着它至少必须使用 进行扩充 SerializableAttribute,并选择性地实现 ISerializable

重要

在自定义内容状态中存储信息时,如果不希望内容保留在内存中,则无法存储对要记住其状态的页面实例的任何引用。 这会阻止 WPF 释放页面实例,并破坏默认导航历史记录行为的用途。 如果必须执行此操作,请考虑改用 KeepAlive

构造函数

CustomContentState()

初始化 CustomContentState 类的新实例。

属性

JournalEntryName

获取存储在导航历史记录中的内容的名称。 的值 JournalEntryNameNavigationWindowFrame和 浏览器导航 UI 中显示。

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
Replay(NavigationService, NavigationMode)

在发生导航时调用,以对内容片段重新应用状态。

ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅