Bewerken

Delen via


CustomContentState Class

Definition

CustomContentState enables the ability to navigate through different states of a single piece of source content without reloading the source content for each subsequent navigation.

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
Inheritance
CustomContentState
Attributes

Examples

The following is an example of a CustomContentState implementation that overrides JournalEntryName.

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

Remarks

By default, NavigationService does not store an instance of a content object in navigation history. Instead, NavigationService creates a new instance of the content object each time it is navigated to by using navigation history. This behavior is designed to avoid excessive memory consumption when large numbers and large pieces of content are being navigated to. Consequently, the state of the content is not remembered from one navigation to the next. However, WPF provides the ability to associate a piece of custom state with the navigation history entry for a piece of content.

Custom state that is associated with a navigation history entry must be a class that derives from CustomContentState. You associate a CustomContentState object with a navigation history entry by using one of the following techniques:

Note

If you call the AddBackEntry method, you must handle the Navigating event or implement IProvideCustomContentState.

When the navigation history entry is navigated to, WPF checks to see if a custom CustomContentState object is associated with it. If so, it calls Replay to allow the custom CustomContentState object to apply the state it remembered from the previous navigation.

A custom CustomContentState class can override JournalEntryName to change the name that appears for the navigation history entry to which the CustomContentState object is associated. The value that JournalEntryName returns is visible from the navigation UI of the various navigators (browser, NavigationWindow, or Frame).

A class that derives from CustomContentState must be serializable, which means it must at least be augmented with SerializableAttribute, and optionally implement ISerializable.

Important

When you store information in custom content state, you cannot store any references to the instance of the page for which you are remembering state if don't want the content to be retained in memory. This prevents WPF from releasing the page instance, and defeats the purpose of the default navigation history behavior. If you must do this, consider using KeepAlive instead.

Constructors

CustomContentState()

Initializes a new instance of the CustomContentState class.

Properties

JournalEntryName

Gets the name for the content that is stored in navigation history. The value of JournalEntryName is displayed from NavigationWindow, Frame, and browser navigation UI.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
Replay(NavigationService, NavigationMode)

Called to reapply state to a piece of content when navigation occurs.

ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

See also