Page.KeepAlive 屬性

定義

取得或設定值,這個值表示 Page 執行個體是否要保留在巡覽記錄中。

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

屬性值

如果 Page 執行個體要保留在巡覽記錄中,則為 true,否則為 false。 預設為 false

範例

下列範例示範如何使用 XAML,在多個導覽之間保留 類別的 Page 實例。

<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

備註

第一次流覽頁面時,就會建立 類別的新實例 Page 。 當頁面離開 () 時,頁面的專案會新增至流覽歷程記錄。 根據預設,專案不會參考頁面物件。 相反地,專案會包含頁面的套件統一資源識別項 (URI) 。 當頁面的專案流覽至使用流覽歷程記錄時,會使用套件 URI 來建立頁面的新實例。 此行為是預設值,以避免使用過多的記憶體:保留頁面實例可以快速取用記憶體,特別是具有非固定內容數量的記憶體。 此問題會藉由無法限制可以儲存在巡覽歷程記錄的返回和向前堆疊中的專案數目來增強。 相反地,儲存頁面的套件 URI 幾乎不會影響記憶體耗用量。

建立頁面新實例的主要副作用是頁面狀態不會從某個頁面實例記住到另一個頁面實例。 在這些情況下,Windows Presentation Foundation提供數種用來記住狀態的技術。

若要讓頁面保持運作,您可以將 屬性設定 KeepAlivetrue (預設值 false 為) 。

注意

例如,呼叫 Navigate) 時,只會使用程式碼 (來具現化和流覽至 的頁面會自動保持運作。

除非您需要下列專案,否則您應該避免將 設定 KeepAlivetrue 為 :

  • 當頁面有大量內容時,可能需要很長的時間才能具現化。 如果頁面未保持運作,而且頁面經常流覽至 ,則持續具現化頁面的成本可能會對使用者體驗產生負面影響。 不過,從效能觀點來看,您應該依賴預設設定和分析應用程式的效能;如果測試識別載入時間低於應用程式所需的範圍的頁面,則設定要保持運作的頁面可能是解決問題的一種方式。

注意

如果使用者離開並返回 XAML 瀏覽器 (應用程式, (XBAP) ,則不會保留在 XAML 瀏覽器應用程式的流覽歷程記錄中) 。 只有未保持運作之頁面的記錄項目會保留在流覽歷程記錄中。

相依性屬性資訊

識別碼欄位 KeepAliveProperty
設定為 的中繼資料屬性 true

注意

這個相依性屬性上的元資料類型為 PropertyMetadata ,而不是 FrameworkPropertyMetadata

適用於

另請參閱