Udostępnij za pośrednictwem


Remember a Single Item of State Across Page Instances

This sample illustrates how to remember a single item of page state across page instances by using a dependency property.

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

Download sample

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. It is possible for WPF to retain page instances in navigation history, although this can be costly from a memory consumption perspective.

However, WPF provides an infrastructure that allows you to store state information with a page entry in navigation history. In this case, you can use the stored state to reconstitute the state of the page after it is instantiated.

This sample shows how to use a journaled dependency property to store and retrieve page state using navigation history. A journaled dependency property is a dependency property that has the following metadata:

Journal

This technique is most useful when you need to store a single item of page state across page instances. To remember a single set of state across page instances, you should consider using IProvideCustomContentState (see Remember a Single Set 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 noteImportant 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.

See Also

Tasks

Remember a Single Set of State Across Page Instances

Remember Multiple Sets of State per Page Instance