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
屬性

範例

以下是覆寫 JournalEntryName的實作CustomContentState範例。

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 方法,則必須處理 Navigating 事件或實 IProvideCustomContentState作 。

瀏覽歷程記錄專案巡覽至 時,WPF 會檢查自定義物件是否 CustomContentState 與其相關聯。 如果是,它會呼叫 Replay 以允許自定義 CustomContentState 物件套用先前導覽所記住的狀態。

自定義 CustomContentState 類別可以覆寫 JournalEntryName ,以變更對象相關聯之導覽歷程記錄專案 CustomContentState 所顯示的名稱。 傳回的值 JournalEntryName 可從瀏覽器、 NavigationWindowFrame) 各種導覽 (器的導覽UI中看到。

衍生自 CustomContentState 的類別必須可串行化,這表示它至少必須以 SerializableAttribute增強,並選擇性地實 ISerializable作 。

重要

當您將資訊儲存在自定義內容狀態時,如果不想將內容保留在記憶體中,則無法儲存您記住狀態之頁面實例的任何參考。 這可防止 WPF 釋放頁面實例,並破壞預設導覽歷程記錄行為的目的。 如果您必須這樣做,請考慮改用 KeepAlive

建構函式

CustomContentState()

初始化 CustomContentState 類別的新執行個體。

屬性

JournalEntryName

取得儲存在瀏覽歷程記錄中之內容的名稱。 的值 JournalEntryName 會從 NavigationWindowFrame和瀏覽器瀏覽 UI 顯示。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Replay(NavigationService, NavigationMode)

在發生巡覽時呼叫,以重新套用狀態至內容。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱