ערוך

שתף באמצעות


Page.KeepAlive Property

Definition

Gets or sets a value that indicates whether the Page instance is retained in navigation history.

public:
 property bool KeepAlive { bool get(); void set(bool value); };
public bool KeepAlive { get; set; }
member this.KeepAlive : bool with get, set
Public Property KeepAlive As Boolean

Property Value

true if the Page instance is retained in navigation history; otherwise, false. The default is false.

Examples

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>
<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"
    >
</Page>
using System;
using System.Windows;
using System.Windows.Controls;

namespace CSharp
{
    public partial class HomePage : Page
    {
        public HomePage()
        {
            InitializeComponent();

            // Keep this page in navigation history
            this.KeepAlive = true;
        }
    }
}

Imports System.Windows
Imports System.Windows.Controls

Namespace VisualBasic
    Partial Public Class HomePage
        Inherits Page
        Public Sub New()
            InitializeComponent()

            ' Keep this page in navigation history
            Me.KeepAlive = True
        End Sub

    End Class
End Namespace

Remarks

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 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 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 offers several techniques for remembering state.

To keep a page alive, you set the KeepAlive property to true (the default is false).

Note

Pages that are instantiated and navigated to using only code (for example, calling Navigate), are automatically kept alive.

You should avoid setting KeepAlive to 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.

Note

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.

Dependency Property Information

Identifier field KeepAliveProperty
Metadata properties set to true None

Note

The metadata type on this dependency property is PropertyMetadata, not FrameworkPropertyMetadata.

Applies to

See also