Gets or sets a value that indicates whether the Page instance is retained in navigation history.
Assembly: PresentationFramework (in PresentationFramework.dll)
When a page is first navigated to, a new instance of the Page class is created. When a page is navigated away from (either back or forward), an entry for the page is added to navigation history. By default, the entry does not reference the page object. Instead, the entry contains a pack uniform resource identifier (URI) for the page. When the entry for the page is navigated to using navigation history, the pack uniform resource identifier (URI) is used to create a new instance of the page. This behavior is the default, to avoid excessive memory use: retaining page instances can quickly consume memory, particularly those with a nontrivial amount of content. This problem is augmented by the fact that there is no limit to the number of entries that can be stored in the back and forward stacks of navigation history. In contrast, storing pack uniform resource identifiers (URIs) for pages has virtually no impact on memory consumption.
The main side effect of creating new instances of a page is that page state is not remembered from one instance of a page to another. In these cases, Windows Presentation Foundation (WPF) offers several techniques for remembering state.
To keep a page alive, you set theproperty to true (the default is false).
Pages that are instantiated and navigated to using only code (for example, calling Navigate), are automatically kept alive.
You should avoid settingto true unless you need to:
When a page has a lot of content, it may take a long time to instantiate. If the page is not kept alive, and the page is frequently navigated to, the cost of constantly instantiating the page may have a negative impact on the user experience. However, from a performance perspective, you should rely on the default settings and profile your application's performance; if testing identifies pages with load times that fall below the range required for your application, configuring the pages to be kept alive may be one way to solve the problem.
Entries for pages that are kept alive are not retained in navigation history of an XAML browser application (XBAP) if a user navigates away from and back to the XAML browser application (XBAP). Only journal entries for pages that are not kept alive are retained in navigation history.
The following example shows how to use XAML to retain an instance of the Page class across multiple navigations.
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" WindowTitle="HomePage" KeepAlive="True" >
<Page x:Class="CSharp.HomePage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" WindowTitle="HomePage" >
Available since 3.0