Remember a Single Set of State Across Page Instances
This sample illustrates how to remember multiple items of page state across page instances by using IProvideCustomContentState and CustomContentState.
This sample demonstrates a specific feature of the Windows Presentation Foundation and, consequently, does not follow application development best practices. For comprehensive coverage of Windows Presentation Foundation (WPF) and Microsoft .NET Framework application development best practices, refer to the following as appropriate:
Accessibility - Accessibility Best Practices
Localization - WPF Globalization and Localization Overview
Performance - Optimizing WPF Application Performance
Security - Windows Presentation Foundation Security
Building the Sample
Install the Windows Software Development Kit (SDK) and open its build environment command window. On the Start menu, point to All Programs, Microsoft Windows SDK, and then click CMD Shell.
Download the sample, usually from the software development kit (SDK) documentation, to your hard disk drive.
To build the sample from the build environment command window, go to the source directory of the sample. At the command prompt, type MSBUILD.
To build the sample in Microsoft Visual Studio, load the sample solution or project file and then press CTRL+SHIFT+B.
Running the Sample
To run the compiled sample from the build environment command window, execute the .exe file in the Bin\Debug or Bin\Release folder contained under the sample source code folder.
To run the compiled sample with debugging in Visual Studio, press F5.
Remarks
By default, Windows Presentation Foundation (WPF) does not store page instances in navigation history. Instead, a new instance of a page is created each time it is navigated to. Subsequently, any state information on the first instance of the page is not remembered when the page is navigated to again. It is possible for WPF to retain page instances and, implicitly, page state in navigation history, although this can be costly from a memory consumption perspective.
However, WPF provides an infrastructure that allows you to both store state information with a page entry in navigation history and reapply it to the new page instance when navigated to using navigation history.
This sample shows how a page can implement IProvideCustomContentState to allow it to remember a single set of state, which may contain one or more items. When a page is navigated away from, WPF calls GetContentState to ask the page for state that it wants to be remembered. GetContentState returns an subclass of CustomContentState, which you create to store and replay state.
This technique is most useful when you need to store several items of page state across page instances. To remember a single item of state across page instances, you should consider using a journalable dependency property (see Remember a Single Item of State Across Page Instances). To remember multiple sets of state for a single page instance, see Remember Multiple Sets of State per Page Instance.
Important Note: |
---|
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. This prevents WPF from releasing the page instance, and defeats the purpose of the default navigation history behavior. If you must do this, you might consider using KeepAlive instead. |